File Watcher for continuous running program
Show older comments
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
Accepted Answer
More Answers (2)
rahul patel
on 9 Jul 2018
0 votes
Hi Elizabeth VanDenburgh how can i read sub folders ?
1 Comment
Elizabeth Reese
on 9 Jul 2018
You may try this FileSystemWatcher documentation which mentions an "IncludeSubdirectories" property that you can set to true.
rahul patel
on 27 Sep 2018
0 votes
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
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!