File Watcher for continuous running program

27 次查看(过去 30 天)
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

采纳的回答

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

请先登录,再进行评论。

更多回答(2 个)

rahul patel
rahul patel 2018-7-9
Hi Elizabeth VanDenburgh how can i read sub folders ?

rahul patel
rahul patel 2018-9-27
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

类别

Help CenterFile Exchange 中查找有关 Naming Conventions 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by