Rename TIF files in a folder
3 views (last 30 days)
Show older comments
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
0 Comments
Answers (2)
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'))
newfilename=sprintf('Camera%04d%s', fileNum - 100000)
oldname= strcat(filename,ext)
rename = strcat(newfilename,ext)
movefile(oldname,rename);
6 Comments
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
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
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)
See Also
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!