How to check if a directory(folder) exists?
Show older comments
I am trying to create folders using mkdir, but if I have already created that folder, how do I check that before creating it. a = mkdir(directorylocation,text1) returns 1 in variable a if the directorylocation\text1 is created and if the directory exists. What is the function or condition that checks whether directorylocation\text1 exists
Accepted Answer
More Answers (2)
Cosmo
on 4 Aug 2020
if not(isfolder(yourFolder))
mkdir(yourFolder)
end
2 Comments
In case anyone else was wondering why this solution is better:
To check the existence of a file or folder, you also can use the isfolder or isfile functions. exist searches for files and folders on the search path, which can lead to unexpected results. isfolder and isfile search for files or folders only on the specified path or in the current folder, which can lead to clearer and faster results.
linhtuptit
on 15 Nov 2023
This has helped me a lot.
7==exist(name,'dir') % check if folder exists
or
7~=exist(name,'dir') % check if folder does NOT exist
To avoid searching the entire MATLAB Search Path use an absolute file/folder name:
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!