フィルターのクリア

Renaming folders

17 ビュー (過去 30 日間)
ch basit
ch basit 2011 年 4 月 14 日
コメント済み: matlab noob 2019 年 4 月 10 日
I have many folder within a folder. I want to rename these folders in such a way that a string " 1-4 " is added to the end of all the folders name. And if any folder already has 1-4 at end leave that folder and change the names for others.
Example A1_ A2_ A3_ A4_1-4 A5_
Renamed folders A1_1-4 A2_1-4 A3_1-4 A4_1-4 A5_1-4

回答 (2 件)

Andrew Newell
Andrew Newell 2011 年 4 月 14 日
  2 件のコメント
Walter Roberson
Walter Roberson 2011 年 4 月 14 日
Does that work for folders (directories) ?
Andrew Newell
Andrew Newell 2011 年 4 月 14 日
Yes.

サインインしてコメントする。


Jarrod Rivituso
Jarrod Rivituso 2011 年 4 月 15 日
I had a problem just like this once and I found recursion to be very helpful (though you have to of course be careful to avoid infinite recursion).
Here's an example function I just created. It essentially does a depth-first recursion down the directory tree, changing directory names as it goes.
function changeDirNames(dirName)
dirResult = dir(dirName);
allDirs = dirResult([dirResult.isdir]);
allSubDirs = allDirs(3:end);
for i = 1:length(allSubDirs)
thisDir = allSubDirs(i);
thisDirName = thisDir.name;
if ~strcmp(thisDirName(end-2:end),'1-4')
oldname = fullfile(dirName,thisDir.name);
newname = [fullfile(dirName,thisDir.name) '1-4'];
movefile(oldname,newname);
end
changeDirNames(newname);
end
To call it, use something like this:
changeDirNames(pwd)
  2 件のコメント
ch basit
ch basit 2011 年 4 月 21 日
Thanks a lot
matlab noob
matlab noob 2019 年 4 月 10 日
If I would like to add 'Patient(1 spacing)' before the exsisting folder name, how do I rewrite the "(3:end)" or "if ~strcmp(thisDirName(end-2:end),'Patient')".
Thank you.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeStartup and Shutdown についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by