Rename TIF files in a folder
조회 수: 1 (최근 30일)
이전 댓글 표시
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
답변 (2개)
sai charan sampara
2024년 7월 30일
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
Walter Roberson
2024년 7월 31일
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
2024년 7월 30일
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
Image Analyst
2024년 7월 31일
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)
참고 항목
카테고리
Help Center 및 File Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!