How to rename a bunch of files in a folder
    19 views (last 30 days)
  
       Show older comments
    
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...
 
    0 Comments
Answers (5)
  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
 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. 
  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
 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
 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 :)
  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
0 Comments
  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 
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!





