Rename TIF files in a folder

3 views (last 30 days)
Tomer
Tomer on 30 Jul 2024
Commented: Image Analyst on 31 Jul 2024
Hello. I have 21841 TIFF images in a folder with names in the format Camera10001, Camera10002,....Camera121841. I want to rename these files as:
Camera10001 to Camera0001, .... Camera121841 to Camera21841.
I used "renamer" software but it gives error from file number 9999 i.e., Camera19999 and so on. I am trying to do the same in MATLAB but the error is still persistent.
% Specify the input and output directories
input_directory = 'D:\Images\Test\Camera_1';
output_directory = 'D:\Images\Test\Camera_1';
% Ensure the output directory exists
if ~exist(output_directory, 'dir')
mkdir(output_directory);
end

Answers (2)

sai charan sampara
sai charan sampara on 30 Jul 2024
Hello Tomer,
You can extract the file names using "dir" function. Then by looping through each file use regex expressions to generate the new name of the file. Then use the "movefile" function to rename the files.
files = dir('D:\Images\Test\Camera_1'); % for getting list of files
[~,filename,ext]=fileparts(files.name(idx)) % for getting name of eaxh file. Index over the range of 1:length(files)
Here is an example code
filename='Camera123001'; %example file name
ext='.tif';
fileNum=str2double(regexp(filename, '\d+', 'match'))
fileNum = 123001
newfilename=sprintf('Camera%04d%s', fileNum - 100000)
newfilename = 'Camera23001'
oldname= strcat(filename,ext)
oldname = 'Camera123001.tif'
rename = strcat(newfilename,ext)
rename = 'Camera23001.tif'
movefile(oldname,rename);
  6 Comments
Walter Roberson
Walter Roberson on 31 Jul 2024
mainFolder = 'D:\Images\Test\Camera_1';
filePattern = fullfile(mainFolder, '*.*');
files = dir(filePattern); % for getting list of files
for idx = 1 : length(files)
fullFileName = fullfile(files(idx).folder, files(idx).name);
[~,filename,ext]=fileparts(fullFileName) % for getting name of each file.
fileNum=str2double(regexp(filename, '\d+', 'match'))
newfilename=sprintf('Camera%04d%s', fileNum - 100000)
oldname= strcat(filename,ext)
rename = strcat(newfilename,ext)
movefile(oldname,rename);
end
Tomer
Tomer on 31 Jul 2024
Edited: Tomer on 31 Jul 2024
Thanks @Walter Roberson. I get this error, after which the code crashes:
ext = '.'
fileNum = []
newfilename = 'Camera0000'
oldname = '.'
rename = 'Camera0000.'
Error using movefile
The process cannot access the file because it is being used by another process.

Sign in to comment.


Govind KM
Govind KM on 30 Jul 2024
Hi Tomer,
It looks like you want to rename these TIFF files to remove the ‘1’ after the starting string of ‘Camera’ for each file. You can do this using the ‘movefile’ function in MATLAB.
% Define the folder where the TIFF images are located
folderPath = 'D:\Images\Test\Camera_1';
% Get a list of all TIFF files in the folder
fileList = dir(fullfile(folderPath, 'Camera1*.tiff'));
% Loop through each file and rename it
for k = 1:length(fileList)
% Get the old file name
oldFileName = fileList(k).name;
newFileName = replace(oldFileName,'Camera1','Camera');
% Get the full file paths
oldFilePath = fullfile(folderPath, oldFileName);
newFilePath = fullfile(folderPath, newFileName);
% Rename the file
movefile(oldFilePath, newFilePath);
end
You can refer to the documentation of the ‘movefile’ function below:
  6 Comments
Tomer
Tomer on 31 Jul 2024
Edited: Tomer on 31 Jul 2024
Thank you all for your kind comments. I used this approach with the corrections suggested, the error is that: Found 12953 tif files after renaming. Initially in the foldere there were 21841 TIf files but afterwards, I see only 12953 images. I made some modifications and created lof files.
Old: D:\Binarized_Images\Test\Camera_1\Camera111112.tif -> New: D:\Binarized_Images\Test\Camera_1\Camera11112.tif Status: Skipped (file exists)
Old: D:\Binarized_Images\Test\Camera_1\Camera111113.tif -> New: D:\Binarized_Images\Test\Camera_1\Camera11113.tif Status: Skipped (file exists)
Old: D:\Binarized_Images\Test\Camera_1\Camera111114.tif -> New: D:\Binarized_Images\Test\Camera_1\Camera11114.tif Status: Skipped (file exists)
Some files are skipped, and I am unable to fix it.
close all; clear all; clc
% Define the folder where the TIFF images are located
folderPath = 'D:\Binarized_Images\Test\Camera_1';
% Get a list of all TIFF files in the folder
fileList = dir(fullfile(folderPath, 'Camera1*.tif'));
fprintf('Found %d tif files in "%s".\n', numel(fileList), folderPath);
% Initialize the log for renamed files and skipped files
fileRenamingLog = cell(length(fileList), 3);
skippedFiles = {};
% Loop through each file and rename it
for k = 1:length(fileList)
% Get the old file name
oldFileName = fileList(k).name;
% Generate the new file name by replacing 'Camera1' with 'Camera'
newFileName = ['Camera', oldFileName(8:end)]; % Remove the '1' after 'Camera'
% Get the full file paths
oldFilePath = fullfile(folderPath, oldFileName);
newFilePath = fullfile(folderPath, newFileName);
% Store old and new file names in the log
fileRenamingLog{k, 1} = oldFilePath;
fileRenamingLog{k, 2} = newFilePath;
% Check if new file path already exists (to avoid overwriting)
if exist(newFilePath, 'file') == 2
fileRenamingLog{k, 3} = 'Skipped (file exists)';
skippedFiles{end+1} = oldFilePath; %#ok<AGROW>
fprintf('Skipped "%s" because "%s" already exists.\n', oldFilePath, newFilePath);
else
fileRenamingLog{k, 3} = 'Renamed';
% Rename the file
movefile(oldFilePath, newFilePath);
fprintf('Renamed "%s" to "%s".\n', oldFilePath, newFilePath);
end
end
% Check the number of files after renaming
newFileList = dir(fullfile(folderPath, 'Camera*.tif'));
fprintf('Found %d tif files after renaming.\n', numel(newFileList));
% Save the renaming log to a text file for debugging
logFilePath = fullfile(folderPath, 'file_renaming_log.txt');
fid = fopen(logFilePath, 'w');
for k = 1:length(fileRenamingLog)
fprintf(fid, 'Old: %s -> New: %s Status: %s\n', fileRenamingLog{k, 1}, fileRenamingLog{k, 2}, fileRenamingLog{k, 3});
end
fclose(fid);
% Save skipped files log
skippedLogFilePath = fullfile(folderPath, 'skipped_files_log.txt');
fid = fopen(skippedLogFilePath, 'w');
for k = 1:length(skippedFiles)
fprintf(fid, 'Skipped: %s\n', skippedFiles{k});
end
fclose(fid);
fprintf('Renaming complete. Log saved to %s\n', logFilePath);
fprintf('Skipped files log saved to %s\n', skippedLogFilePath);
Image Analyst
Image Analyst on 31 Jul 2024
What was the name of one it skipped?
Try moving them to a different output folder instead of renaming in place.
One potential problem about running the code over and over again in the same folder is that if the number begins with Camera11*** then getting rid of the first 1 will leave you with a file that still begins with Camera1 and it will be renamed again if you run the code again.
Maybe instead of the old
if exist(newFilePath, 'file') == 2
try
if isfile(newFilePath)

Sign in to comment.

Categories

Find more on Get Started with MATLAB in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!