mkdir/ exist conflict in for-loop

I have code that checks for existance of folders and then creates them if needed, e.g.:
for k=1:16
this_folder = sprintf('valid_path_info'); %on a network drive
if ~exist(this_folder,'dir')
mkdir(this_folder)
end
end
When I start, none of the folders exist. For most of the cases (15/16 for the above example), the folders get created as expected. However, one of the cases reports (in debugger):
dir_exists = exist(this_folder); % returns 0, as expected
mkdir(this_folder)% fails and returns a warning that the directory already exists
Has anyone come across this or know of a good workaround?
For reference, I'm running Matlab 2011B on a Windows7 pc

Réponses (2)

Sean de Wolski
Sean de Wolski le 3 Août 2012

0 votes

Hidden directory maybe?
winopen(pwd)
And then view hidden files/folders or whatever it is.
Image Analyst
Image Analyst le 3 Août 2012

0 votes

Try it with the 'dir' option like this:
dir_exists = exist(this_folder, 'dir');

2 commentaires

Jon
Jon le 6 Août 2012
I believe that this is not a Matlab issue but a Windows/ network issue.
Here are the results of my quick testing:
dir_exists = exist(this_folder); % returns 0, as expected
dir_not_exists = ~exist(this_folder); % also returns 0
Since I was checking for non-existance, it then tried to create the folder, which apparently existed in some partial state. I got the same results when I used a command prompt in Windows, so my ultimate workaround was simply to create a new directory :-|
Sean de Wolski
Sean de Wolski le 6 Août 2012
Jon, please add this as an answer and accept it so this question is closed and so future people can see that it worked for you!

Connectez-vous pour commenter.

Catégories

En savoir plus sur File Operations dans Centre d'aide et File Exchange

Question posée :

Jon
le 3 Août 2012

Community Treasure Hunt

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

Start Hunting!

Translated by