Detect New File in a Directory
Afficher commentaires plus anciens
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 commentaires
Walter Roberson
le 13 Oct 2011
The mechanisms for doing this are operating-system specific. Please indicate which OS you are using.
lynniz
le 14 Oct 2011
Réponse acceptée
Plus de réponses (2)
Dimitrios Patikas
le 27 Août 2022
Modifié(e) : Dimitrios Patikas
le 27 Août 2022
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
le 27 Août 2022
0 votes
Catégories
En savoir plus sur File Operations dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!