listening to the creation/deletion of a file with specific name.
조회 수: 8 (최근 30일)
이전 댓글 표시
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
Jan
2018년 5월 10일
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
2018년 5월 10일
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.
채택된 답변
Guillaume
2018년 4월 30일
편집: Guillaume
2018년 4월 30일
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
Guillaume
2018년 5월 9일
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.
추가 답변 (1개)
Jose Luis Susa Rincon
2018년 8월 17일
편집: Voss
2024년 10월 30일 15:50
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
Walter Roberson
2018년 8월 18일
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.
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!