listening to the creation/deletion of a file with specific name.

36 views (last 30 days)
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 Comments
Guillaume
Guillaume on 10 May 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.

Sign in to comment.

Accepted Answer

Guillaume
Guillaume on 30 Apr 2018
Edited: Guillaume on 30 Apr 2018
Yes, with .Net it's very easy using System.IO.FileSystemWatcher. See this recent question where I show how to use it. Of course, it is indeed windows only.
For other platforms, Java has an equivalent class, the WatchService. It seems to be more complicated to use that the .Net equivalent, and I'm not very well versed in java, so I couldn't figure out all the details. Using the example from there, this is how it would start:
fs = java.nio.file.FileSystems.getDefault;
watcher = fs.newWatchService;
javapath = fs.getPath('C:\somewhere\somefolder', 'somefilename');
At this point you're supposed to be able to register the watchservice but I get a no method with matching signature error that I haven't got the time now to dig into to solve
javapath.register(watcher, java.nio.file.StandardWatchEventKinds.ENTRY_MODIFY) %does not work
But hopefully, this should get you started.
edit: fat fingers
  19 Comments
Arabarra
Arabarra on 10 May 2018
I see... well, that's too bad.
In any case, even if they don't control over what is available, they could offer a clean wrapper on top of whatever engine would be actually running below. I guess could end figuring out how to use python watchdog, or java watchers or whatever, but I'm certain that Mathworks engineers would make a much better job at it :-)

Sign in to comment.

More Answers (1)

Jose Luis Susa Rincon
Jose Luis Susa Rincon on 17 Aug 2018
Edited: Jose Luis Susa Rincon on 18 Aug 2018
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('shit it changed!!!')
end
  1 Comment
Walter Roberson
Walter Roberson on 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.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by