필터 지우기
필터 지우기

renaming a lot of folders automatically by MATLAB

조회 수: 8 (최근 30일)
mohammad
mohammad 2011년 9월 20일
It's needed renaming 150 folders depends on numbers of .xls files inside their contents for example inside folder 'a' there are 15 .xls files so needed be renamed to 'a(15)' and in folder 'b' there are 45 .xls file so its needed renaming it to 'b(45)' and so on.but this must happen by using for-loop. by using this function names of folder is gathered : http://www.mathworks.com/matlabcentral/fileexchange/30835-getcontents
but is there any command for renaming a folder?(not file) and how could for-loop be wrote?

채택된 답변

Jan
Jan 2011년 9월 20일
Folders can be renamed using MOVEFILE.
The FOR loop without GETCONTENTS (which I do not know):
ADir = dir(PathName);
ADir = ADir([ADir.isdir]);
AName = {ADir.name};
AName(strncmp(AName, '.', 1)) = [];
for iFolder = 1:numel(AName)
APath = fullfile(PathName, AName{iFolder});
BDir = dir(fullfile(APath, '*.xls'));
newName = sprintf('%s(%d)', AName{iFolder}, numel(BDir));
movefile(APath, fullfile(PathName, newName));
end
  댓글 수: 1
mohammad
mohammad 2011년 9월 20일
great! perfect!
really so much thanks Jan
I appreciate you for your so nice helping. you helped and answered me so much today

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

추가 답변 (1개)

Tigersnooze
Tigersnooze 2011년 9월 20일
Assuming you have the names of all of the folders in question, try something like:
for i = 1:length(directories)
cd(directories(i));
xlsFiles = dir('*.xls');
num_xlsFiles = length(dir.name);
system(['mv ' directories(i) ' ' directories(i) '(' num2str(num_xlsFiles) ')']);
end
That also assumes you're on a Linux/UNIX machine. If you're on Windows, replace "mv" in the system command with "move" (at least I think that's the Windows equivalent). I think that should work.
  댓글 수: 3
Jan
Jan 2011년 9월 20일
f "directories" is a cell string, you need curly braces in the CD and SYSTEM command. You need to convert num_xlsFiles to a string before you can use it to create the new folder name, e.g. by using SPRINTF or NUM2STR.
Matlab's built-in command MOVEFILE works on all platforms and is much faster than forwarding a string to the operating system.
Tigersnooze
Tigersnooze 2011년 9월 20일
Realized the num2str mistake just after posting and remedied that. Also forgot about the movefile command, but listed the syntax for that in my comment. Not sure about the format of "directories," since I made it up, but I suppose it might be a cell array.

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

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by