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.
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 个评论
采纳的回答
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 个评论
更多回答(2 个)
rahul patel
2018-7-9
Hi Elizabeth VanDenburgh how can i read sub folders ?
1 个评论
Elizabeth Reese
2018-7-9
You may try this FileSystemWatcher documentation which mentions an "IncludeSubdirectories" property that you can set to true.
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
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Naming Conventions 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!