MATLAB: Rename jpg files in a folder

rename

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,

Best Answer

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