MATLAB Answers

Rename jpg files in a folder

12 views (last 30 days)
I need some help here,
So I have like 200 Images with the name of 1 2 3 4 ... n
Ineed the file to be renamed as 001 002 .... 012... 034 .... 180 ... 200
How to do that?
Thanks,

Accepted Answer

Image Analyst
Image Analyst on 23 Oct 2020
Try this:
folder = pwd; % Wherever your input images are stored.
outputFolder = fullfile(folder, '/renamed'); % Can be the same as the input folder if you want.
% Make output folder if it exists.
if ~isfolder(outputFolder)
mkdir(outputFolder)
end
fileList = dir('*.bmp')
% Loop over all files.
for k = 1 : length(fileList)
% Get this filename.
thisFileName = fullfile(fileList(k).folder, fileList(k).name);
% Find out just the base filename without the folder.
[~, baseFileNameNoExt, ext] = fileparts(thisFileName);
number = str2double(baseFileNameNoExt);
% Skip non-numbers
if isnan(number)
continue;
end
fprintf('Processing %s...\n', thisFileName);
% Optionally display the image
% % Read in the input image.
% originalImage = imread(thisFileName);
% % Display it.
% imshow(originalImage);
% drawnow;
% Create the new base filename without the folder.
baseOutputFileName = sprintf('%3.3d%s', number, ext);
% Create output image filename....
fprintf(' Renaming to %s to %s...\n', baseFileNameNoExt, baseOutputFileName);
% Create the output filename which will be in the output folder.
outputFileName = fullfile(outputFolder, baseOutputFileName);
movefile(thisFileName, outputFileName); % or imwrite()
end
message = sprintf('Done processing %d images', length(fileList))
uiwait(helpdlg(message));
If you want to save the renamed files in a different folder, use imwrite(), otherwise if you want to rename in the same folder use movefile().

  2 Comments

Image Analyst
Image Analyst on 25 Oct 2020
Ravin - are you still there? Did it work or not?
Ravin Rayeok
Ravin Rayeok on 25 Oct 2020
Hey sorry just checked it today, it did work.
Thank you very very much!

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!