need a script to rename and resort TIF files
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentare
Akzeptierte Antwort
Jan
am 18 Okt. 2023
Bearbeitet: Jan
am 18 Okt. 2023
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
2 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu File Operations finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!