File Watcher for continuous running program
27 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Munsell Randall
il 1 Dic 2017
Risposto: rahul patel
il 27 Set 2018
Hello,
I'm trying to write a script that will watch a file system (folder with sub folders) and run a function every time a new file is automatically generated from another system and placed in the watched directory. I also need the script to listen for files that are copied into the watched directory.
once this works, i plan on compiling it as a standalone .exe that will run 24/7 and process new files.
Please let me know what I am missing here to keep it running. Below is what I have so far, it does absolutely nothing when i copy a new file into the directory:
function continual_process()
process = true;
while process
path_to_watch = 'C:\Users\3cal-shape\Desktop\test';
fileObj = System.IO.FileSystemWatcher(path_to_watch);
fileObj.Filter = '*.slm';
fileObj.EnableRaisingEvents = true;
addlistener(fileObj,'Created', @eventhandlerChanged);
addlistener(fileObj,'Changed', @eventhandlerChanged);
end
end
function eventhandlerChanged(source,arg) disp(source) disp('found new file') end
0 Commenti
Risposta accettata
Elizabeth Reese
il 4 Dic 2017
the post that you linked to is using this functionality in a figure, so the callbacks are used as long as the figure is running. The mention of the infinite loop in that post refers to making your script run continuously, not recreating the fileObj repeatedly. In order to do this, remove the while loop and move to after the final addlistener. This will leave the fileObj as it was created, but continue listening as long as the loop is running.
For example:
function continual_process()
path_to_watch = 'C:\Users\3cal-shape\Desktop\test';
fileObj = System.IO.FileSystemWatcher(path_to_watch);
fileObj.Filter = '*.slm';
fileObj.EnableRaisingEvents = true;
addlistener(fileObj,'Created', @eventhandlerChanged);
addlistener(fileObj,'Changed', @eventhandlerChanged);
i=0;
while i<1000000
i = i+1;
pause(0.001);
end
end
function eventhandlerChanged(source,arg)
disp(source)
disp('found new file')
end
3 Commenti
Più risposte (2)
rahul patel
il 9 Lug 2018
Hi Elizabeth VanDenburgh how can i read sub folders ?
1 Commento
Elizabeth Reese
il 9 Lug 2018
You may try this FileSystemWatcher documentation which mentions an "IncludeSubdirectories" property that you can set to true.
rahul patel
il 27 Set 2018
HI Elizabeth VanDenburgh,
I have the following code to process the when a *.mf4 file is created at specified path(including sub folders) Here i have a pause time of 1 min. when i execute the code it calls the callback function immediately. Please let me know how to change the code to call the call back at 1 min (any specified time) rather than immediately when a file is created. What is the point of the pause function here if its not waiting for 60sec before calling the call back function.
function Real_Time_Processing_Main() %---------------------------------------------------------------------% %Read the 'File_Watcher_Path.txt' text file and get the path to watch %---------------------------------------------------------------------% fid_Watcher = fopen('File_Watcher_Path.txt','r'); Directory_to_Watch = textscan(fid_Watcher,'%s'); fclose(fid_Watcher); %---------------------------------------------------------------------% path_to_watch = char(Directory_to_Watch{1}) fileObj = System.IO.FileSystemWatcher(path_to_watch); fileObj.IncludeSubdirectories = true; fileObj.Filter = '*.mf4'; fileObj.EnableRaisingEvents = true; addlistener(fileObj,'Created', @eventhandlerChanged); Infinate_loop = true while Infinate_loop pause(60.0); end end
function eventhandlerChanged(source,arg)
Safety_Events_Array = {}; Miles_Final_Array ={}; %----------------------------------------% % Extract full file name and path %----------------------------------------% Full_Path_mf4_File = char(arg.FullPath); [filepath,name,ext] = fileparts(Full_Path_mf4_File); Mf4_File_Folder = filepath; Mf4_File_Name = strcat(name,ext); %----------------------------------------% % Call Processing 1 %----------------------------------------% %----------------------------------------% % Call Processing 2 %----------------------------------------% %----------------------------------------% % Call Processing 3 %----------------------------------------% end
0 Commenti
Vedere anche
Categorie
Scopri di più su Naming Conventions in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!