need a script to rename and resort TIF files
2 次查看(过去 30 天)
显示 更早的评论
I have a group of TIF files with serial such as:
Im_000000
Im_000001
Im_000002
Im_000003
Im_000004
and I need to rename and resort them to be as follow
Im_000000.B
Im_000001.A
Im_000001.B
Im_000002.A
Im_000002.B
i use the below script, but it can only to rename the file with A and B without sorting
clear all; clc;
INDIV = 'C:\Users\Desktop\New folder\';
filenames_INDIV = dir(fullfile(INDIV, '*.tif'));
for filenum= 1 : numel(filenames_INDIV);
if rem(filenum,2) == 0
[~, basename] = fileparts(filenames_INDIV(filenum).name);
newname = sprintf('%s_%04d-imgA.tif', basename, filenum(:));
movefile(fullfile(INDIV, filenames_INDIV(filenum).name), fullfile(INDIV, newname));
end
if rem(filenum,2) == 1
[~, basename] = fileparts(filenames_INDIV(filenum).name);
newname = sprintf('%s_%04d-imgB.tif', basename, filenum(end,:));
movefile(fullfile(INDIV, filenames_INDIV(filenum).name), fullfile(INDIV, newname));
end
end
0 个评论
采纳的回答
Jan
2023-10-18
编辑:Jan
2023-10-18
Folder = 'C:\Users\Desktop\New folder\';
Files = dir(fullfile(Folder, '*.tif'));
Keys = {'A', 'B'};
[~, name] = fileparts(Files{1});
name = name(1:find(name == '_', 1, 'last')); % Correct assumption?
for iFile = 1:numel(Files)
newname = sprintf('%s_%06d%s.tif', ...
name, ... % 'Im_07291355_'
ceil((iFile - 1) / 2), ... % '000000', '000001', '000001', ...
Keys{rem(iFile, 2) + 1} ); % 'B' or 'A'
disp(newname) % Try it at forst before uncommenting:
% movefile(fullfile(Folder, Files(iFile).name), fullfile(Folder, newname));
end
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 File Operations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!