Renaming folders with different names

8 ビュー (過去 30 日間)
Eva Balgova
Eva Balgova 2019 年 5 月 24 日
編集済み: Rik 2019 年 5 月 24 日
Hi,
I am new to MATLAB, so please bear with me. I am currently trying to loop through folders and rename subfolders within them. The code below seems to work if the name of the subfolder is same in each folder, but I am not sure how to rename subfolders that are named differently. Any suggestions?
The code I use is:
cd ('D:\ToM_3_LEVEL1_ONLY');
for n=1:9
cd (['ToM_00', sprintf('%d',n)]);
movefile (['ANALYSYS_DT_REG_P000_24_05_2019'], ['24_05_19_ANALYSYS_DT_REG_P00', sprintf('%d',n)]);%here is the problem with MATLAB not recognising the file to be renamed. The last number is different for each folder...e.g.20191, 20192 etc.
cd ../..
end
  2 件のコメント
Guillaume
Guillaume 2019 年 5 月 24 日
I would recommend you never use cd. You can pass the full paths of the folders to movefile.
Which pattern of names are you trying to rename to which other pattern?
Eva Balgova
Eva Balgova 2019 年 5 月 24 日
Many thanks for your reply.
The subfolders to rename are: 'ANALYSYS_DT_REG_P000_24_05_2019' and I would like to rename them to '24_05_19_ANALYSYS_DT_REG_P00'. They are in the ToM_00 main folders.

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

回答 (1 件)

Rik
Rik 2019 年 5 月 24 日
編集済み: Rik 2019 年 5 月 24 日
You can use something like this
%untested code
list=dir('D:\ToM_3_LEVEL1_ONLY\ToM_00\*');
list=list([list.isdir]);%keep only folders
for n=1:numel(list)
old_name=list(n).name;
%replace by something like regexp if needed
date=old_name([(end-9):(end-4) (end-1):end]);%remove 20 from year
name=old_name(1:(end-10));
new_name=[date '_' name];
movefile(old_name,new_name);
end

カテゴリ

Help Center および File ExchangeFile Operations についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by