question to continue the code
3 次查看(过去 30 天)
显示 更早的评论
I would like to create a code that creates a folder, verifies if it already exists and if it occurs asks the user if continue or not.
If the folder already exists, two options should be available:
- overwrite the folder (delete it and remake)
- terminate the code
My code is:
name='Folder';
loadcase='11';
if ~exist([name,loadcase], 'dir') % create saving folder
mkdir([name,loadcase]);
else
prompt=('The folder already exists. Do you want to overwrite the analysis?');
risp=input(prompt,'s');
if risp=='Y'
rmdir([name,loadcase]);
mkdir([name,loadcase]);
else
finish
end
end
3 个评论
Walter Roberson
2020-2-25
Probably what to put in in place of finish
If you were to put this into a function then return from the function.
采纳的回答
Rik
2020-2-25
As the documentation states, rmdir only works for empty folders. If you want to remove all files and folders inside that folder you will have to use the s switch:
rmdir([name,loadcase],'s');
doc:
rmdir folderName s also attempts to remove all subfolders and files in folderName, regardless of their write permissions. The result for read-only files follows the practices of the operating system.
0 个评论
更多回答(0 个)
另请参阅
类别
在 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!