필터 지우기
필터 지우기

Creating Subfolders in a loop?

조회 수: 6 (최근 30일)
Sharif Khalil
Sharif Khalil 2018년 8월 27일
편집: Stephen23 2018년 8월 28일
Hi, I have one folder, and need to create 52 subfolders using a for loop, I guess newSubFolder line needs to be modified
for CatNo = 1:52
newFolder = sprintf('/Categories%d', 'C:\Users\sheri\Desktop');
if ~exist(newFolder,'dir')
mkdir C:\Users\sheri\Desktop Categories;
end
[parentFolder, deepestFolder] = fileparts('C:\Users\sheri\Desktop\Categories');
newSubFolder = sprintf('%s/Category#CatNo%d','C:\Users\sheri\Desktop\Categories',deepestFolder);
if ~exist(newSubFolder,'dir')
mkdir (newSubFolder);
end
end

채택된 답변

Stephen23
Stephen23 2018년 8월 28일
편집: Stephen23 2018년 8월 28일
Your code has several features that need to be fixed, such as your using %d format specifier with a character vector, use of the loop iterator name within the format string, getting rid of pointless duplication of data, and moving the main folder creation out of the loop. Probably you want something like this:
D = 'C:\Users\sheri\Desktop';
F = 'Categories';
if ~exist(fullfile(D,F),'dir')
mkdir(fullfile(D,F))
end
for k = 1:52
Z = fullfile(D,F,sprintf('Category%d',k));
if ~exist(Z,'dir')
mkdir(Z)
end
end
  댓글 수: 2
Sharif Khalil
Sharif Khalil 2018년 8월 28일
편집: Stephen23 2018년 8월 28일
Thank you so much, it works perfectly!
Sean de Wolski
Sean de Wolski 2018년 8월 28일
In newer releases you can change exist(Z,'dir') to isfolder which is a little easier to read.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by