Renaming Files based on a Folder Name in Sequence

11 ビュー (過去 30 日間)
Naomi Gaggi
Naomi Gaggi 2018 年 10 月 2 日
コメント済み: Naomi Gaggi 2018 年 10 月 2 日
Hi, I am trying to do the following: I have folders participant 1, participant 2, etc. I want to name the files xxxx-folder name. Each file name is different but begins with 0001_. How can I do this in a loop (for multiple folders)?
  2 件のコメント
Akira Agata
Akira Agata 2018 年 10 月 2 日
> I have folders participant 1, participant 2, etc. I want to name the files xxxx-folder name.
You mean, for example, files in the 'participant 1' folder shall be renamed to '0001_participant 1.***', '0002_participant 1.***',...and so on? ('***' is extension of each file)
Naomi Gaggi
Naomi Gaggi 2018 年 10 月 2 日
編集済み: Naomi Gaggi 2018 年 10 月 2 日
Yes, thanks - or it would even be helpful to just add participant 1 at the end of each file name

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

採用された回答

Akira Agata
Akira Agata 2018 年 10 月 2 日
Thank you for the clarification. Then, the program would be something like this.
s = dir('./participant*/*.*');
prevDir = '';
for kk = 1:numel(s)
if ~s(kk).isdir
oldName = s(kk).name;
[~,~,ext] = fileparts(oldName);
[~,currDir] = fileparts(s(kk).folder);
if ~strcmp(s(kk).folder,prevDir)
pref = 1;
prevDir = s(kk).folder;
else
pref = pref + 1;
end
newName = sprintf('%04d_%s%s',pref,currDir,ext);
movefile(fullfile(s(kk).folder,oldName),fullfile(s(kk).folder,newName));
end
end
  1 件のコメント
Naomi Gaggi
Naomi Gaggi 2018 年 10 月 2 日
Thanks, so much this was so helpful!!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by