question to continue the code

조회 수: 4 (최근 30일)
Gaetano Pavone
Gaetano Pavone 2020년 2월 25일
답변: Rik 2020년 2월 25일
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:
  1. overwrite the folder (delete it and remake)
  2. 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
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.
Gaetano Pavone
Gaetano Pavone 2020년 2월 25일
It doesn't work for non-empty folders

댓글을 달려면 로그인하십시오.

채택된 답변

Rik
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개)

카테고리

Help CenterFile Exchange에서 Environment and Settings에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by