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

22 vues (au cours des 30 derniers jours)
Arabarra
Arabarra le 30 Avr 2018
Commenté : Walter Roberson le 18 Août 2018
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 commentaires
Jan
Jan le 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
Guillaume le 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.

Connectez-vous pour commenter.

Réponse acceptée

Guillaume
Guillaume le 30 Avr 2018
Modifié(e) : Guillaume le 30 Avr 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 commentaires
Guillaume
Guillaume le 9 Mai 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
Arabarra le 10 Mai 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 :-)

Connectez-vous pour commenter.

Plus de réponses (1)

Jose Luis Susa Rincon
Jose Luis Susa Rincon le 17 Août 2018
Modifié(e) : Jose Luis Susa Rincon le 18 Août 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 commentaire
Walter Roberson
Walter Roberson le 18 Août 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.

Connectez-vous pour commenter.

Community Treasure Hunt

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

Start Hunting!

Translated by