フィルターのクリア

Detect New File in a Directory

29 ビュー (過去 30 日間)
lynniz
lynniz 2011 年 10 月 13 日
回答済み: Walter Roberson 2022 年 8 月 27 日
Hi All,
Last time when I google this question, I actually found a function that does what I need, but I couldn't find it this time.
Basically what I need is to monitor a directory for any new files. If there are new files, I will read the files in and continue to monitor for new files.
Any help will be greatly appreciated!
Thank you,
Lynniz
  2 件のコメント
Walter Roberson
Walter Roberson 2011 年 10 月 13 日
The mechanisms for doing this are operating-system specific. Please indicate which OS you are using.
lynniz
lynniz 2011 年 10 月 14 日
I have Windows 7. Another question is do OS always return files in an alphabetical order?
Thanks,
Lynniz

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

採用された回答

Honglei Chen
Honglei Chen 2011 年 10 月 13 日
Hi lynniz,
You can use dir to list all the files under a certain directory, cache it and constantly query the folder and use setdiff to see whether a new file showed up. The following example shows a brief example. Note that I'm assuming everything underneath the directory is a regular file. If you have subdirectories, you can filter them out using the isdir field in the result of dir.
dir_content = dir;
filenames = {dir_content.name};
current_files = filenames;
while true
dir_content = dir;
filenames = {dir_content.name};
new_files = setdiff(filenames,current_files);
if ~isempty(new_files)
% deal with the new files
current_files = filenames;
else
fprintf('no new files\n')
end
end
Of course you may want to define an exit flag to get you out of the loop easily.
HTH
  3 件のコメント
Fangjun Jiang
Fangjun Jiang 2011 年 10 月 13 日
What if file name keeps the same but it is newly updated? You might need to check the time stamp of the file, compare it to the last time that the checking is done, to see if it is "new".
lynniz
lynniz 2011 年 10 月 14 日
Thanks for the inputs. I will monitor the time stamp as well.

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

その他の回答 (2 件)

Dimitrios Patikas
Dimitrios Patikas 2022 年 8 月 27 日
編集済み: Dimitrios Patikas 2022 年 8 月 27 日
To avoid looping over and over again, you may use a timer function and keep it working on the background unless younstom the timer. An example to get always the newest file:
t = timer;
t.TimerFcn = @timerFunc;
t.Period = 0.6; % how often you want to check
t.ExecutionMode = 'fixedRate';
t.start % use t.stop to stop the timer
function timerFunc(~,~)
persistent CurrentNewest
Path = 'A/path/you/want/to/keep/track';
CurrentNewest = 'NewestFilename.new'
ListFiles = dir(Path);
ListFiles = ListFiles(~[ListFiles.isdir]);
[~, idx] = sort([ListFiles.datenum]);
newest = ListFiles(idx(end));
if ~strcmp(newest.name, CurrentNewest)
disp(['The newest file is now: ', newest.name])
CurrentNewest = newest.name;
end
end

Walter Roberson
Walter Roberson 2022 年 8 月 27 日

カテゴリ

Help Center および File ExchangeFile Operations についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by