Effacer les filtres
Effacer les filtres

need a script to rename and resort TIF files

3 vues (au cours des 30 derniers jours)
Mohamed
Mohamed le 17 Oct 2023
Commenté : Mohamed le 18 Oct 2023
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

Réponse acceptée

Jan
Jan le 18 Oct 2023
Modifié(e) : Jan le 18 Oct 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 commentaires
Mohamed
Mohamed le 18 Oct 2023
I tried the script, but this error apears to me:
>> [~, name] = fileparts(Files{1});
Brace indexing is not supported for variables of this type.
Mohamed
Mohamed le 18 Oct 2023
Many thanks, it works!!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Structures dans Help Center et File Exchange

Tags

Produits


Version

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by