How to rename a bunch of files in a folder

166 views (last 30 days)
Oleg Komarov
Oleg Komarov on 22 Feb 2011
Answered: Hannah Rodger on 10 May 2022

I was going to merge .pdf files with Adobe Acrobat when I noticed they were named as 1.pdf, 2.pdf, ..., 10.pdf, ..., 20.pdf.

The main drawback of this kind of naming is that you have to reorder the files manually (see pic below).

As an alternative I thought: why don't I just rename the files padding the names with zeros to guarantee the 'right' ordering up to hundreds of files such that 1.pdf --> 001.pdf, 10.pdf --> 010.pdf etc...

Answers (5)

Jiro Doke
Jiro Doke on 22 Feb 2011
Here's an example:
% Get all PDF files in the current folder
files = dir('*.pdf');
% Loop through each
for id = 1:length(files)
% Get the file name (minus the extension)
[~, f] = fileparts(files(id).name);
% Convert to number
num = str2double(f);
if ~isnan(num)
% If numeric, rename
movefile(files(id).name, sprintf('%03d.pdf', num));
end
end
  1 Comment
Ade Aulya
Ade Aulya on 11 Dec 2018
hi.. thank you for this. i tried to use this code and it were run, but i didn't get the name file as i want. i wanted to change those file's number into only 1,2,3,4,5,and so on.. FYI, i've tried to change the folder name like this..
% Get all files in the current folder clear all;
files = dir(fullfile('C:\Users\ASUS\Desktop\2','*.tif'));
% Loop through each
for id = 1:length(files)
% Get the file name (minus the extension)
[~, f] = fileparts(files(id).name);
% Convert to number
num = str2double(f);
if ~isnan(num)
% If numeric, rename
APath = fullfile('C:\Users\ASUS\Desktop\2', files(id).name);
movefile(APath, sprintf('%d.tif', num));
end
end
and the output files name were like this :
2.100000e+00.tif
2.100100e+00.tif
2.100200e+00.tif
2.100300e+00.tif
2.100400e+00.tif
2.100500e+00.tif
etc..
or if i want to change it like
1a
2a
3a
continue directly to
5b
6b
7b
and so on..
what should i do, please ?
thank you so much.

Sign in to comment.


Oleg Komarov
Oleg Komarov on 22 Feb 2011
I came up with the following solution:
% Directory of the files
d = 'C:\Users\Oleg\Desktop\New folder\';
% Retrieve the name of the files only
names = dir(d);
names = {names(~[names.isdir]).name};
% Calculate the length of each name and the max length
len = cellfun('length',names);
mLen = max(len);
% Exclude from renaming the files long as the max
idx = len < mLen;
len = len(idx);
names = names(idx);
Core of the script:
% Rename in a LOOP
for n = 1:numel(names)
oldname = [d names{n}];
newname = sprintf('%s%0*s',d,mLen, names{n});
dos(['rename "' oldname '" "' newname '"']); % (1)
end
Alternative methods, repace (1):
movefile(oldname, newname)
java.io.File(oldname).renameTo(java.io.File(newname))
Or use Simon's FileRename
Some considerations on the timings might be found on CSSM: Why is MOVEFILE so slow?
I thought to add this as answer, do you have more suggestions?
Oleg

Sameer Suregaonkar
Sameer Suregaonkar on 1 Dec 2016
Edited: Sameer Suregaonkar on 1 Dec 2016
This a refined and clear version of Jiro Doke's code. Thanks to Jiro Doke for the core program.
% Get all files in the current folder clear all;
files = dir(fullfile('E:','imgs1','1','*.tif'));
% Loop through each
for id = 1:length(files)
% Get the file name (minus the extension)
[~, f] = fileparts(files(id).name);
% Convert to number
num = str2double(f);
if ~isnan(num)
% If numeric, rename
APath = fullfile('E:','imgs1','1', files(id).name);
movefile(APath, sprintf('%03d.tif', num));
end
end
  2 Comments
Ade Aulya
Ade Aulya on 11 Dec 2018
hi.. thank you so much for clear codes. i used to use your code it were run but i didn't get the name file as i want. if i wanted to change those file's number into only 1,2,3,4,5,and so on.. what should i do, please ? FYI, i tried to change the folder name like this..
% Get all files in the current folder clear all;
files = dir(fullfile('C:\Users\ASUS\Desktop\2','*.tif'));
% Loop through each
for id = 1:length(files)
% Get the file name (minus the extension)
[~, f] = fileparts(files(id).name);
% Convert to number
num = str2double(f);
if ~isnan(num)
% If numeric, rename
APath = fullfile('C:\Users\ASUS\Desktop\2', files(id).name);
movefile(APath, sprintf('%d.tif', num));
end
end
and the output files name were like this :
2.100000e+00.tif
2.100100e+00.tif
2.100200e+00.tif
2.100300e+00.tif
2.100400e+00.tif
2.100500e+00.tif
etc..
do you have any idea, pease ?
thankyou.
Ade Aulya
Ade Aulya on 11 Dec 2018
or if i want to change it like 1a,2a,3a,4a,5a continue directly to 6b,7b,8b,9b,10b, and so on..
could u please give me an idea ?
thank you again :)

Sign in to comment.


Sulaymon Eshkabilov
Sulaymon Eshkabilov on 9 Jun 2020
Here is the answer that changes the file names to: 1a.tif, 2a.tif, 3a.tif, ..., etc.
files = dir('*.tif');
for ii = 1:length(files)
% Get the file name (minus the extension)
[~, fname] = fileparts(files(ii).name);
% Convert to number
N = str2num(fname);
if ~isnan(N)
movefile(files(ii).name, sprintf('%2da.tif', N));
end
end

Hannah Rodger
Hannah Rodger on 10 May 2022
I am trying to rename a group of files using the bottom line in each file as the new name for the individual files. is there a way to do this in matlab?
Thank
Hannah

Categories

Find more on Search Path 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!