依次将文件夹中名为1,2,3,...,10,11,...的文件重命名为001,002,003,...,010,011,... [英] Rename files named 1, 2, 3,..., 10, 11, ... in folder to 001, 002, 003,..., 010, 011,... in that order

查看:469
本文介绍了依次将文件夹中名为1,2,3,...,10,11,...的文件重命名为001,002,003,...,010,011,...的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在文件夹中有一组文件,它们的名称分别为1、2、3,...,10、11 ...,并且我正在这些文件上运行MATLAB代码,并且正在接收这些文件如1、10、11、12,...(错误的顺序),这是我所不希望的. 我只想按顺序1、2、3,...获取文件. 那么,有没有一种方法可以在MATLAB中执行此操作(我正在使用dir()命令来获取文件夹中的所有文件)?

I have a set of files in a folder and they are named as 1, 2 , 3, ..., 10, 11,... and I am running a MATLAB code on these files and it is taking the files as 1, 10, 11, 12,...(wrong order) which I don't want. I want to get the files in the sequence 1, 2, 3, ... only. So, is there a way to do this in MATLAB (I am using dir() command to get all the files of the folder)?

我的MATLAB代码如下:

My MATLAB code goes as follows:

file_names= dir('DirContainingFiles1,2,3,...');
for imgj=1: length(file_names)
    file= file_names(imgj).name;
    ......
    ......
end

因此,上面的这个文件变量应该按顺序1、2、3,...获取每个循环中的所有文件. 但是它以1,10,11,...序列(基于文本的方案)进入. 请帮助按编号顺序获取它.

So, this file variable above is supposed to get all the files in each loop in a sequence 1, 2, 3,... But it is getting in 1, 10, 11,... sequence (text based scheme). Please help in getting it in numbered sequence.

推荐答案

这是MATLAB解决方案:

Here is a MATLAB solution:

cd DirContainingFiles1,2,3,...
names = strsplit(ls);
[~,idx]=sort(str2double(names));
for name = names(idx)
    disp(name{1})
    ....
end

您不需要重命名文件.使用 ls 获取文件列表.将它们转换为数字格式,并获取已排序元素的索引.

You don't need to rename files. Get list of the files using ls. Convert them to numeric format and get index of the sorted elements.

如果要使用dir:

file_names= dir('DirContainingFiles1,2,3,...'); 
names = {file_names(3:end).name};
[~,idx]=sort(str2double(names));
for name = names(idx)
    disp(name{1})
    ....
end

这篇关于依次将文件夹中名为1,2,3,...,10,11,...的文件重命名为001,002,003,...,010,011,...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆