Why is the command dir inconsistent about recognizing directories?
2 views (last 30 days)
Show older comments
Hi All,
I have an application that stores setup data in a specific folder. When it starts it looks to see if that folder exists so it can load the current data. If not it creates the folder and saves default settings. My script was working just fine last week, and today for some reason, it doesn't recognize the folder that it creates as a directory. Specifically if I call d=dir;, and i is the instance of the folder (that I can see in Windows Explorer), then d(i).name is the name of the folder, but d(i).isdir returns a 0. Here's some of the code:
d = dir;
create_data_folder = 1;
for i=1:length(d)
fprintf('The name is %s and isdir is %d\n',d(i).name,d(i).isdir)
if d(i).isdir
if strcmp(d(i).name, 'Folder Name')
create_data_folder = 0;
fprintf('Starting up.\n');
end
end
end
After this, if create_data_folder is 1, it uses a mkdir command to create the folder "Folder Name". Once it's there, sometimes it recognizes it as a directory and sometimes it doesn't! I'd appreciate it if anyone has any ideas. Thanks.
Glenn
1 Comment
Cedric
on 9 Apr 2013
Did you try to see what EXIST returns using the 'kind' argument 'dir' and 'file'?
Accepted Answer
Image Analyst
on 9 Apr 2013
Here's an alternate way using genpath:
startingFolder = pwd; % or wherever...
folderList = genpath(startingFolder)
baseFolderToSearchFor = '\tests3'
folderToCreate = fullfile(startingFolder, baseFolderToSearchFor)
if strfind(lower(folderList), lower(baseFolderToSearchFor)) > 1
fprintf('Found the %s folder\n', folderToCreate);
else
fprintf('Did not find the %s folder.\nWill create it.\n', folderToCreate);
% mkdir(folderToCreate);
end
More Answers (0)
See Also
Categories
Find more on File Operations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!