Rename TIF files in a folder

조회 수: 2 (최근 30일)
Tomer
Tomer 2024년 6월 4일
댓글: Les Beckham 2024년 6월 8일
Hi. I have 21841 TIF format files (in a folder) with names like: Camera_6_100fps_20240403_0956560001, and so to Camera_6_100fps_20240403_09565621841. I want to rename them to the format Camera60001 so on to Camera621841. I have tried a for loop but did not succeed. Can someone please help me with this.
Thanks.
  댓글 수: 2
Michael VanMeter
Michael VanMeter 2024년 6월 4일
Can you post the for loop that you tried?
Tomer
Tomer 2024년 6월 4일
% This code is to rename the file names
% Define the directory containing the TIF files
sourceDir = 'G:\Expt\Camera_6'; % Update this path to your directory
% Get a list of all TIF files in the directory
filePattern = fullfile(sourceDir, '*.tif');
tifFiles = dir(filePattern);
% Loop through each file and rename it
for k = 1:length(tifFiles)
% Get the current filename
baseFileName = tifFiles(k).name;
fullFileName = fullfile(sourceDir, baseFileName);
% Extract the index part from the filename
indexStr = baseFileName(end-7:end-4);
% Construct the new filename
newFileName = ['Camera6', indexStr, '.tif'];
newFullFileName = fullfile(sourceDir, newFileName);
% Rename the file
movefile(fullFileName, newFullFileName);
% Display the old and new file names
fprintf('Renamed %s to %s\n', fullFileName, newFullFileName);
end

댓글을 달려면 로그인하십시오.

채택된 답변

Les Beckham
Les Beckham 2024년 6월 4일
system('touch Camera_6_100fps_20240403_0956560001.tif'); % create files for testing
system('touch Camera_6_100fps_20240403_09565621841.tif');
files = dir('*.tif');
for iFile = 1:numel(files)
fn = files(iFile).name;
idx = find(fn == '_', 1, 'last') + 6; % + 6 to skip the timestamp
newfn = [strrep(fn(1:8), '_', '') fn((idx+1):end)]; % pull out the parts of the filename we want to keep
movefile(fn, newfn)
end
newfiles = dir('*.tif');
newfiles(:).name
ans = 'Camera60001.tif'
ans = 'Camera621841.tif'
  댓글 수: 2
Tomer
Tomer 2024년 6월 5일
Thank you.
Les Beckham
Les Beckham 2024년 6월 8일
You are quite welcome.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Michael VanMeter
Michael VanMeter 2024년 6월 4일
You haven't provided enough detail on how you're attempting to rename these files, but a simple for loop using movefile should be sufficient. You can modify the dest (destination) variable as needed to get the desired naming convention.
files = dir('Camera_6*.tif');
[~, I] = sort([files.datenum]);
files = files(I);
fileNum = 60001;
for file = files'
src = fullfile(file.folder,file.name);
dest = fullfile(file.folder,['Camera',num2str(fileNum),'.tif']);
movefile(src, dest)
fileNum = fileNum + 1;
end
  댓글 수: 2
Michael VanMeter
Michael VanMeter 2024년 6월 4일
Slightly different approach using regular expression.
files = dir('*.tif');
exp = 'Camera_\d+_\d+fps_\d+_\d{5}(\d+)\.tif';
for file = files'
matches = regexp(file.name,exp,'tokens','once');
dest = ['Camera',matches{1},'.tif'];
movefile(file.name, dest)
end
Tomer
Tomer 2024년 6월 5일
Thank you.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 File Operations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by