I have group of images 500, and there are named from Image1, Image2, ... Image500.I Want to rename them to Box_1_Image1, Box_2_Image2,...Box_500_Image500.I Also want to keep the same sequence as of original folder and put renamed images in same folder.

3 Kommentare

path_to_folder = 'path_to_your_folder';
image_files = dir(fullfile(path_to_folder, 'Image*.jpg')); % Change ".jpg" as necessary
for i = 1:numel(image_files)
% Generate the new filename
new_filename = sprintf('Box_%d_%s', i, image_files(i).name);
% Rename the file
movefile(fullfile(folder, image_files(i).name), fullfile(path_to_folder, new_filename));
end
Raza Ali
Raza Ali am 9 Feb. 2024
Bearbeitet: Raza Ali am 9 Feb. 2024
Thank you for the answer, but the output is not accurate, Its like Box_1_Image1, Box_2_Image10, Box_45_Image5,Box_46_Image50. Any many others are also wrong...
Stephen23
Stephen23 am 9 Feb. 2024
Bearbeitet: Stephen23 am 9 Feb. 2024
" but the output is not accurate, Its like Box_1_Image1, Box_2_Image10, Box_45_Image5,Box_46_Image50. Any many others are also wrong..."
To fix that you could download and use my function NATSORTFILES:
"I Also want to keep the same sequence as of original folder"
Files in a folder do not really have a well-defined sequence. They might appear in character-code order, but that is not guaranteed by any file management system that I am aware of. The order that they are displayed in is purely an artifact of the application displaying them, e.g. Windows Explorer.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Stephen23
Stephen23 am 9 Feb. 2024
Bearbeitet: Stephen23 am 9 Feb. 2024

0 Stimmen

P = '.'; % absolute or relative path to where the files are saved.
F = 'image*.bmp'; % filename with wildcard and file extension
S = dir(fullfile(P,F));
for k = 1:numel(S)
tmp = regexprep(S(k).name,'^\D+(\d+)(.+)$','Box_$1_Image$1$2');
new = fullfile(S(k).folder,tmp);
old = fullfile(S(k).folder,S(k).name);
movefile(old,new)
end
Note how it gets the numbers matched correctly (the order here is due to DIR, is not relevant):
dir *.bmp
Box_10_Image10.bmp Box_1_Image1.bmp Box_20_Image20.bmp Box_2_Image2.bmp Box_3_Image3.bmp
If you want to sort filenames into alphanumeric order then you can use my function NATSORTFILES:

Kategorien

Mehr zu Programming finden Sie in Hilfe-Center und File Exchange

Gefragt:

am 9 Feb. 2024

Bearbeitet:

am 9 Feb. 2024

Community Treasure Hunt

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

Start Hunting!

Translated by