Monitoring a directory for new files does not catch all the files

5 ビュー (過去 30 日間)
Marcin Kowalski
Marcin Kowalski 2023 年 3 月 7 日
コメント済み: Voss 2023 年 3 月 8 日
Hi all,
I need to monitor a directory for new files, specifically images, that are appearing in a specific location. In order to do this I am using simple code as below:
folder = 'path_to_directory'
imds_start = imageDatastore(folder,"IncludeSubfolders",true);
filenames = imds_start.Files;
current_files = filenames;
while true
imds = imageDatastore(folder,"IncludeSubfolders",true);
filenames = imds.Files;
new_files = setdiff(filenames,current_files);
if ~isempty(new_files)
fprintf('ONE NEW FILE')
current_files = filenames;
filename = char(new_files(end));
IMpath = strcat(folder,filename)
else
% fprintf('no new files\n')
end
end
The issue is that when files appear very quickly, some of the are not "detected". I assume that reading the file content is not the best option but this is the only one I came up with that actually works in MATLAB. Would you propose something more efficient?

採用された回答

Voss
Voss 2023 年 3 月 7 日
new_files will contain the names of all the new files, so instead of using just new_files(end) and assuming there's one new file, use all of new_files.
if ~isempty(new_files)
fprintf('%d NEW FILE(S)',numel(new_files))
% now do what you're going to do with new_files (not just new_files(end))
end
  2 件のコメント
Marcin Kowalski
Marcin Kowalski 2023 年 3 月 8 日
yeah, simple as that! I don't know why I didn't notice this :) Thank you, it works!
Voss
Voss 2023 年 3 月 8 日
You're welcome!

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

その他の回答 (0 件)

カテゴリ

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

製品


リリース

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by