How to rename a bunch of files in a folder

173 ビュー (過去 30 日間)
Oleg Komarov
Oleg Komarov 2011 年 2 月 22 日
回答済み: Hannah Rodger 2022 年 5 月 10 日

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...

回答 (5 件)

Jiro Doke
Jiro Doke 2011 年 2 月 22 日
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 件のコメント
Ade Aulya
Ade Aulya 2018 年 12 月 11 日
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.

サインインしてコメントする。


Oleg Komarov
Oleg Komarov 2011 年 2 月 22 日
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
  1 件のコメント
Isabella Osetinsky-Tzidaki
Isabella Osetinsky-Tzidaki 2020 年 6 月 2 日
Thank you, great solution, very useful.

サインインしてコメントする。


Sameer Suregaonkar
Sameer Suregaonkar 2016 年 12 月 1 日
編集済み: Sameer Suregaonkar 2016 年 12 月 1 日
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 件のコメント
Ade Aulya
Ade Aulya 2018 年 12 月 11 日
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 2018 年 12 月 11 日
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
Sulaymon Eshkabilov 2020 年 6 月 9 日
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 2022 年 5 月 10 日
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

カテゴリ

Help Center および File ExchangeSearch Path についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by