File Watcher for continuous running program

25 views (last 30 days)
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.
I have been playing with the solution here but it only runs once and doesn't continue listening.
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

Accepted Answer

Elizabeth Reese
Elizabeth Reese on 4 Dec 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 Comments
Munsell Randall
Munsell Randall on 4 Dec 2017
nvm, i think i got it. i used the following:
process = true;
while process
pause(0.001);
end
Munsell Randall
Munsell Randall on 4 Dec 2017
how do i make this work for when the directory i am watching includes subfolders?

Sign in to comment.

More Answers (2)

rahul patel
rahul patel on 9 Jul 2018
Hi Elizabeth VanDenburgh how can i read sub folders ?
  1 Comment
Elizabeth Reese
Elizabeth Reese on 9 Jul 2018
You may try this FileSystemWatcher documentation which mentions an "IncludeSubdirectories" property that you can set to true.

Sign in to comment.


rahul patel
rahul patel on 27 Sep 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

Tags

Community Treasure Hunt

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

Start Hunting!