一个文件夹中的文件:
1份(1).jpg,
1份(2).jpg
.......
.......
.......
1份(800)
文件的奇数编号并将其保存在其他文件夹中。所需的输出如下所示:
1.JPG,
3.JPG,
5.JPG
...
...
...
799.jpg
我试过的源代码:
a ='C:\Users\rahul\Desktop\jumbled test\copiedimages\';
A =dir( fullfile(a, '*.jpg') );
fileNames = { A.name };
for iFile = 1 : numel( A )
newName = fullfile(a, sprintf( '%d.jpg',iFile+2) );
movefile( fullfile(a, fileNames{ iFile }), newName );
end
但我无法获得所需的输出。那么解决方案是什么?
答案 0 :(得分:2)
不要重命名文件,只需在检查文件名中是否有奇数后将它们移动到新目录:
a ='C:\Users\rahul\Desktop\jumbled test\copiedimages\';
destination = a ='C:\Users\rahul\Desktop\jumbled test\copiedimages\Odd\';
A =dir( fullfile(a, '*.jpg') );
fileNames = { A.name };
for iFile = 1 : numel( A )
[~,name] = fileparts(fileNames{ iFile }); %// This part might not be necessary if fileNames{ iFile } is just the file name without the directory. In that case rather use name=fileNames{ iFile };
%// Extract the number from the file name
filenum = str2num(name(8:end-4));
%// Check if the number is odd
if mod(filenum,2) == 1
movefile(fullfile(a, fileNames{ iFile }), fullfile(destination, fileNames{ iFile }));
end
end