listening to the creation/deletion of a file with specific name.
Ältere Kommentare anzeigen
Hi,
I'm wondering if it is possible somehow to listen to the creation/deletion of a file with an specific name. I know in Windows one has the option of controling .NET events, but I need a similar functionality also in Linux and Mac... I know I can create timers that scan continuously the file system, but this solution is rather inelegant and seems to compromise the performance of my code, so I'm interested in a solution more oriented to actual listeners... Any ideas?
Thanks in advance, Daniel
6 Kommentare
Jan
am 30 Apr. 2018
Using a tiny C-mex function I can check the existence of a (not existing) file more then 40'000 times per second: all you need is an _open(FileName, _O_RDWR). The timer would cause some overhead also, but if 1 sec reaction time is sufficient, I do not see, why this should be inelegant or a performance problem.
Please explain, what should happen in case of a file creation and which reaction time is required.
Steven Lord
am 9 Mai 2018
Be careful with how (and when) you try to process the files that the "watcher" detected.
Suppose your local supermarket is out of your favorite brand of breakfast cereal. The manager tells you that they're getting more soon because the truck carrying the shipment is already on the way. So you camp out watching the supermarket's loading dock with binoculars (you REALLY like this cereal :) and as soon as the truck arrives you rush into the store to the cereal aisle. But the cereal doesn't teleport from the truck to the shelves. You still need to wait for the truck to be unloaded before the cereal is available on the shelf.
In this analogy, the file is the truck carrying the cereal and you plucking a box off the shelf is the operation you want to perform on the file. Just because the truck is at the supermarket (the file is present on the disk) doesn't mean it's available for you to use quite yet.
Arabarra
am 10 Mai 2018
Jan
am 10 Mai 2018
My application controls a database, which can contain thousands of files.
If this is a real data base application, it should be able to trigger events on the file creation. A data base can e.g. lock the file status during the access from the outside. Using the file system and a polling loop is very indirect and prone to timing problems, which can be extremely hard to predict and to debug. Therefore Steven's analogy hits the point: Instead of looking for the truck, let the staff of the supermarket inform you, that your resources are available.
Guillaume
am 10 Mai 2018
Certainly, if you're using a proper database engine, it should have triggers that could be captured more easily in matlab than watching the file system.
Akzeptierte Antwort
Weitere Antworten (1)
Jose Luis Susa Rincon
am 17 Aug. 2018
Bearbeitet: Voss
am 30 Okt. 2024
I found an easy way to check for changes, not the fanciest but it works. the idea is to compare two strings with the dates of when the file changed, if the date changed voila!
function GetFileTime()
DateNumberOld='17-Aug-2018 16:45:51' %this is just initial time to start checking for changes
DateNumberNew=DateNumberOld %the new and old are the same initially
while(DateNumberNew==DateNumberOld) % if new is different than old means that the file changed
fileName='SOMEPATH/SOMEFILE.txt' %file to check
listing = dir(fileName);
% check we got a single entry corresponding to the file
assert(numel(listing) == 1, 'No such file: %s', fileName);
modTime = listing.datenum; %listing is a structure, you can get datenum or any other
DateNumberNew = listing.date %I take date and compare it with the previous date
end
disp('it changed!!!')
end
1 Kommentar
Walter Roberson
am 18 Aug. 2018
I recommend comparing the numeric listing.datenum field instead of the text listing.date field. The text .date field reflects local time, so it is after timezone adjustments, where the numeric datenum is before timezone adjustments. You don't want your code ignoring changes for an hour during the shift back to Standard Time.
Kategorien
Mehr zu Code Performance finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!