MATLAB Answers

Rename jpg files in a folder

24 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?

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)
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)
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()
message = sprintf('Done processing %d images', length(fileList))
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().
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)


Community Treasure Hunt

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

Start Hunting!

Translated by