Rename a lot of files
Show older comments
I have a lot of files with names 'Sample_1.csv, Sample_2.csv, Sample_3.csv, ..., Sample_101'. I would like to rename all of them to the format of Sample_xxx.csv, so it becomes like this: 'Sample_001.csv, Sample_002.csv, Sample_003.csv, ..., Sample_101'.
Can anyone help me? Thank you very much.
Accepted Answer
More Answers (1)
Akira Agata
on 15 Aug 2022
How about the following?
fileList = dir('*.csv');
for kk = 1:numel(fileList)
fileNum = extractBetween(fileList(kk).name,'_','.csv');
fileNum = str2double(fileNum);
newFileName = sprintf("Sample_%03d.csv", fileNum);
movefile(fileList(kk).name, newFileName);
end
1 Comment
Cheuk Yin Wong
on 15 Aug 2022
Categories
Find more on Function Creation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!