Rename files in a directory

36 views (last 30 days)
Tala Hed
Tala Hed on 26 Feb 2018
Commented: Tala Hed on 26 Feb 2018
Dear Experts, I have 50 csv files and want to rename them. The current names are 10.csv, 20.csv, ...700.csv and want to rename them to data1.csv, data2.csv,...data70.csv. They are all in matlab directory. Can you please help me :)

Accepted Answer

Akira Agata
Akira Agata on 26 Feb 2018
Another possible solution:
f = dir('*.csv');
for kk = 1:numel(f)
fileFrom = f(kk).name;
fileTo = ['data',erase(f(kk).name,'0.csv'),'.csv'];
movefile(fileFrom,fileTo);
end

More Answers (1)

Walter Roberson
Walter Roberson on 26 Feb 2018
Adapting the example from the documentation of using that contribution:
D = 'C:\Test';
S = dir(fullfile(D,'*.csv'));
N = natsortfiles({S.name});
for k = 1:numel(N)
sourcefile = fullfile(D, N{k});
destfile = fullfile(D, sprintf('data%d.csv', k));
movefile(sourcefile, destfile);
end

Categories

Find more on Data Import and Analysis 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!