How to rename a bunch of files in a folder
173 ビュー (過去 30 日間)
古いコメントを表示
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 件のコメント
回答 (5 件)
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
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.
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
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
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
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
0 件のコメント
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
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Search Path についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!