How to listen to changes in a directory

12 ビュー (過去 30 日間)
Hans
Hans 2012 年 10 月 30 日
回答済み: Walter Roberson 2022 年 2 月 26 日
Hi everyone,
I would like to listen to changes in a directory such that a callback function is executed if a new file or folder has been created in that directory. Something like
listen_to_foler_changes('c:\testdir', @do_something)
where the function 'do_something' is called when a new folder is created in 'c:\testdir'. Is there an easy solution to that problem?
Thanks in advance Tobi
  4 件のコメント
Hans
Hans 2012 年 10 月 31 日
Thanks all for your suggestions. Unfortunately, I would prefer a solution without polling.
Jan
Jan 2012 年 10 月 31 日
編集済み: Jan 2012 年 10 月 31 日
Please, Hans, add useful tags for your question. Simply ignoring us is not a cute strategy. The tags are sued to classify questions, therefore they support the quality of this forum.
There is no solution without repeated manual checks inside Matlab. And from outside of Matlab, it is hard to start a callback function without another polling mechanism.

サインインしてコメントする。

回答 (3 件)

Jing
Jing 2012 年 10 月 31 日
Hi Hans,
I don't think there's a handle for a directory, so it won't be possible to notify an event for the change of directory. But I think you can imitate the event system to do the same thing.
1.you need to use a timer object to call a function to check the content of the directory:
T = timer('TimerFcn',@mycallback,'Period',0.1); %execute the mycallback function every 0.1 seconds.
start(T);
2. in the callback function of the timer, get the whole content and compare with the previous one:
function mycallback
listing = dir(name);
if ~isequal(listing,prelist)
do_something;
end
prelist=listing;
Notice that the timer will be in execution queue and the period 0.1 second may not be exactly the same as real time goes. And for convenience, you can just use the mycallback function after any code that may change the directory.
  1 件のコメント
Jan
Jan 2012 年 10 月 31 日
  • Checking the disk with a frequency of 0.1 seconds will be too fast. I think 1.0 seconds will be more realistic.
  • The important method to store prelist is not implemented or mentioned.
  • Be aware, that the TIMER's callbacks are executed in their own thread, although this is not documented sufficiently. So extra mechanisms are required to avoid collisions with the Matlab code running in the foreground.

サインインしてコメントする。


Jan
Jan 2012 年 10 月 31 日
編集済み: Jan 2012 年 10 月 31 日
Small but important addition to Jing's code:
UD.Stored = [];
UD.Path = 'c:\testdir';
UD.Callback = @do_something; % Or: {@do_something, ...}
T = timer('TimerFcn', @myTimercCallback, 'Period', 1.0, 'UserData', UD);
start(T);
function myTimerCallback(TimerH, EventH)
UserData = get(TimerH, 'UserData');
thisDir = dir(UserData.Path);
if isempty(UserData.Stored)
UserData.Stored = thisDir;
elseif ~isequal(UserData.Stored, thisDir)
if iscell(UserData.Callback)
feval(UserData.Callback{:});
else % Function handle:
feval(UserData.Callback);
end
end

Walter Roberson
Walter Roberson 2022 年 2 月 26 日
See https://www.mathworks.com/matlabcentral/answers/99277-how-do-i-programmatically-detect-a-change-in-a-directory for several methods

カテゴリ

Help Center および File ExchangeStartup and Shutdown についてさらに検索

製品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by