MATLAB Answers

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

22 views (last 30 days)
Arabarra on 30 Apr 2018
Commented: Walter Roberson on 18 Aug 2018
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


Show 3 older comments
Arabarra on 10 May 2018
haha, good analogy. And good point. Well, actually in my application I'm on the safe side. I'm lucky enough that the action to trigger when a truck is in the supermarket is just to broadcast an announcement notifying customers that the truck is there, and the cereal will come in a short time.
Jan on 10 May 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 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 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


Arabarra on 9 May 2018
thanks for all the usage examples... I agree that this is not as easy as one would like, but well... hopefully Mathworks will offer something similar to the NET system in all platforms at some point...
Guillaume on 9 May 2018
It's bit more complicated than that. Matlab can interface with a number of external runtimes. On Windows, you've got Microsoft .Net and COM because they're built into windows. Microsoft does not provide a .Net runtime for other platforms, hence why it's not supported. There is an open-source effort to provide a .Net runtime: mono.
On all platforms, you've also got Java because the java runtime is cross-platform.
Recently, mathworks added support for python, which is also a cross-platform runtime. In fact, that's another option for you, I've just done a search and there's a watchdog python library.
You can also use perl which is another cross-platform runtime.
In all cases, the runtime is not developed by Mathworks, so they have no control over what is available. I have no inside knowledge over what Mathworks is planning, but I don't see them ever developing a filesystem watcher directly into matlab.
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 = %I take date and compare it with the previous date
disp('shit it changed!!!')

  1 Comment

Walter Roberson
Walter Roberson on 18 Aug 2018
I recommend comparing the numeric listing.datenum field instead of the text 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.

Sign in to answer this question.