Renames a lot of files in directory

조회 수: 4(최근 30일)
Oktavian Jason
Oktavian Jason 2020년 3월 3일
댓글: Oktavian Jason 2020년 3월 3일
Hello, I have a lot of .mat files in a folder and I want to rename it from ex : 65300106179 into something like Flight 10001 and so on. Below is my code
f = dir( fullfile( projectdir, '**', '*.mat') );
for kk = 1:numel(f)
fileFrom = f(kk).name;
fileTo = ['Flight',erase(f(kk).name,'1000%d.mat'),'.mat'];
movefile(fileFrom,fileTo);
end
The problem I have is the file name changed into Flight65300106179.mat. Any idea where I did wrong?

채택된 답변

KSSV
KSSV 2020년 3월 3일
f = dir( fullfile( projectdir, '**', '*.mat') );
for kk = 1:numel(f)
fileFrom = f(kk).name;
fileTo = ['Flight','1000%d.mat','.mat'];
movefile(fileFrom,fileTo);
end
  댓글 수: 6
Oktavian Jason
Oktavian Jason 2020년 3월 3일
Do you have any idea how I can rename on subfolder too?
Example : Folder A : Flight 10001-Flight 10010, Folder B : Flight 10011-Flight 10020

댓글을 달려면 로그인하십시오.

추가 답변(1개)

Bhaskar R
Bhaskar R 2020년 3월 3일
fileTo = sprintf('Flight 1000%d.mat',kk);

Community Treasure Hunt

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

Start Hunting!

Translated by