read a folder that is currently copied to computer

1 view (last 30 days)
Hi! Im currently working on a project where I want to automatic analyse images on a server. The folder is monitored with a while loop and a pause inside. The image analysis is done by a function I made which takes the filename, decides if it is a folder or an image and analyse the images in the folder or the single image. The thing is that it works like a charm until the function tries to analyse a folder that is not fully copied, this is the part I cannot solve! The error message is something about that the file cannot be found. So windows must in some way give the filename when searching through the folder even though the file is not ready. My plan B is to insert a 1 minute pause if the program finds a file.. but there must be some easier way! Have someone encountered a similar problem? Hope this makes sense.. Thanks! /Patrik, Stockholm
Simplified code to illustrate how Im thinking:
stdPath='C:\Temp'; %path where images comes
while(get(hObject,'Value')==1) %togglebutton
listOfPaths = dir(stdPath);
if(length(listOfPaths)>2)
numberOfFiles=length(listOfPaths);
for j=3:numberOfFiles
fullFilename=fullfile(stdPath,listOfPaths(j).name);
analyseThatFilename(fullFilename)
newFullFilename=fullfile('C:\Temp\done',fullFilename)
movefile(fullFilename, newFullFilename)%move folder or image
end
else
disp('No work to be done atm :(')
end
pause(60)
end

Accepted Answer

Jason Ross
Jason Ross on 7 Mar 2012
There are a few ways to tackle this, but they depend on how much control you have over how the files are getting there in the first place.
You can take a page from how a lot of browsers do this kind of thing. They call the file something like "filename.part" and then when the download is complete, it's changed to the proper extension.
You could also poll the number of items in the folder, or the folder size. If it's changing, then you wait a certain number of periods little while before trying to process it. The waiting periods will be dependent on how long your copy takes.
If you have control over what's putting the files there you could always drop a file in there that says "wait I'm still copying" and wait until the file is removed.
  1 Comment
Patrik
Patrik on 7 Mar 2012
That was a good idea about taking the size or number of items, I will try that! I havnt god control over the images (from a CT scanner).
Thanks!

Sign in to comment.

More Answers (1)

Daniel Shub
Daniel Shub on 7 Mar 2012
So many things ...
There are other ways, but I am not sure if they are easier. I would suggest writing a function which embeds the read in a try-catch with the catch waiting a minute and then recursively calling the function.
The other thing is instead of waiting a minute in your main loop, you should look at timer objects.
doc timer

Community Treasure Hunt

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

Start Hunting!