need a script to rename and resort TIF files

3 次查看(过去 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

采纳的回答

Jan
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
  2 个评论
Mohamed
Mohamed 2023-10-18
I tried the script, but this error apears to me:
>> [~, name] = fileparts(Files{1});
Brace indexing is not supported for variables of this type.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 File Operations 的更多信息

标签

产品


版本

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by