How to check if a directory(folder) exists?
조회 수: 1,387 (최근 30일)
이전 댓글 표시
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
댓글 수: 0
채택된 답변
drummer
2018년 10월 20일
well, I suppose you want to create if it doesn't exist, after checking... so you could do it like that:
if ~exist(yourFolder, 'dir')
mkdir(yourFolder)
end
It basically check if youFolder does not exist. If that's true, it creates youFolder.
Cheers.
댓글 수: 2
추가 답변 (2개)
Cosmo
2020년 8월 4일
if not(isfolder(yourFolder))
mkdir(yourFolder)
end
댓글 수: 2
tjueterb
2020년 8월 11일
편집: tjueterb
2020년 8월 11일
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.
참고 항목
카테고리
Help Center 및 File Exchange에서 File Operations에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!