Wait for complete file transfer before running while loop

16 views (last 30 days)
I am using a while loop to monitor a folder for new files. My code process the new files (images and txt in a subfolder) transferred to the folder every 5 min. However, if my loop is initiated while a new folder is still transferred to the monitored folder I get an error. How can I pause the while loop if new files are transferred at the same moment? I was thinking something like sum the files sizes two times and if size 2 is increasing compared to size 1 then pause for 30 sec. But is there a more elegant way to do this? I need to compile my function and therefor not using the .net watch function.

Answers (1)

James Browne
James Browne on 12 Jun 2019
I am by no means an expert in programming but I had an idea that might be a good workaround. Let's say you don't try to wait for the file transfer to be complete, explicity, couldn't you find a way to identify what the last file name should be and check for it then have some logic statement like:
while (~lastFileNameExists)
%read file names;
if (last file == expectedLastFileName)
lastFileNameExists = true;
%process data;
The syntax is obviously not correct but the concept is to trap the processor in the while loop and force it to keep reading the list of file names until it finds that the last file name is present, which indicates that the file transfer is complete. If you don't know what the last file name is going to be (because it is not unique, etc), could you just put a blank file in at the end of the file transfer and look for the blank file's name to appear to indicate that the file transfer is complete?
Hope that at least points you in the right direction~





Community Treasure Hunt

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

Start Hunting!

Translated by