Rename many images using loop function and movefile function

Greeting. Sorry for my ignorance in computer science. I am new in digital image processing. I face problems in rename-ing 20 images. Here is my code:
a ='C:\Users\Ace\Desktop\rename\';
A =dir( strcat (a, '*.bmp' ));
fileNames = { A.name };
for iFile = 1 : numel( A )
newName = sprintf( '%05d.bmp', iFile );
movefile( fileNames{ iFile }, newName );
end
Error message came out: " ??? Error using ==> movefile No matching files were found. "
And I notice the "movefile( fileNames{ iFile }, newName ); " does not run.
Any expert can offer me guidance how to rename file using movefile function? Or I have missed a step or made syntax error?
Thanks.

 採用された回答

Walter Roberson
Walter Roberson 2011 年 5 月 21 日

9 投票

The name field returned by dir() does not include the directory.
a ='C:\Users\Ace\Desktop\rename\';
A =dir( fullfile(a, '*.bmp') );
fileNames = { A.name };
for iFile = 1 : numel( A )
newName = fullfile(a, sprintf( '%05d.bmp', iFile ) );
movefile( fullfile(a, fileNames{ iFile }), newName );
end

7 件のコメント

Eric
Eric 2011 年 5 月 22 日
Hey my friend. It works like a charm! Thanks a lot! Greatly appreciate it!
saif
saif 2012 年 11 月 13 日
great answer walter, thanks
Tilkesh
Tilkesh 2017 年 3 月 8 日
Excellent !!! Good contribution.
Roberto Emanuele Rizzo
Roberto Emanuele Rizzo 2018 年 11 月 6 日
Awesome! Thanks for sharing this. :-)
Gulzar Ahmad
Gulzar Ahmad 2019 年 2 月 12 日
thanks dear
Excellent work
Mahtab Mohammadpoor faskhodi
Mahtab Mohammadpoor faskhodi 2019 年 7 月 17 日
I used this order but I found a problem, my files do not rename in an order. It means that they mixed up. what should I do to make them in the same order that they hade before rename.
Zara Khan
Zara Khan 2022 年 7 月 6 日
Walter Roberson : working but not happening in original order. Please check

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeFile Operations についてさらに検索

質問済み:

2011 年 5 月 21 日

コメント済み:

2022 年 7 月 6 日

Community Treasure Hunt

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

Start Hunting!

Translated by