Change the folder names from one digit to 2 digit numbers

4 ビュー (過去 30 日間)
Butterflyfish
Butterflyfish 2016 年 4 月 26 日
コメント済み: Butterflyfish 2016 年 4 月 27 日
I have a folder 'data' full of folders which are named like this:
E:\data\foldername1\
E:\data\foldername2\
E:\data\foldername3\
...
E:\data\foldername100\
E:\data\foldername11\
...
I would like to rename the folders with one digit (foldername1- foldername9) with two digits, like that:
E:\data\foldername01\
E:\data\foldername02\
...
As I have lots of those 'data' folders I would like to automate it. I tried without success so far... Could anyone please help me?
  2 件のコメント
Azzi Abdelmalek
Azzi Abdelmalek 2016 年 4 月 26 日
If you rename E:\data\foldername1\ to E:\data\foldername10\, then what about the existing E:\data\foldername10\?
Butterflyfish
Butterflyfish 2016 年 4 月 27 日
Sorry I meant foldername01 not 10, will edit the question

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

採用された回答

Alessandro Masullo
Alessandro Masullo 2016 年 4 月 26 日
You can use dir, sscanf, sprintf and movefile.
Use dir to get the list of the folders.
Use scanf to get the number of the folder:
sscanf('folder39','folder%d')
ans =
39
Use sprintf to write the new folder name
sprintf('foldername%04d',15)
ans =
foldername0015
Use movefile to rename your folder.
  2 件のコメント
Butterflyfish
Butterflyfish 2016 年 4 月 27 日
Thank you! Just running into a problem here:
NewName = sprintf(thisDirName '%02d', EndNumber)
Just because my folder names are actually all different to one another I cannot use
sprintf('foldername%04d',15)
Any suggestion?
Butterflyfish
Butterflyfish 2016 年 4 月 27 日
Figured smth out:
NewName = sprintf('%s%02d',thisDirName(1:end-1), EndNumber2)
working now! Thanks a lot for the help

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangePrinting and Saving についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by