MATLAB批量文件重命名详解
阅读原文时间:2021年04月20日阅读:1

简介

做项目的时候需要给大量图片更改名字,800多张图片手动改非得改到吐血。网上搜了重命名的代码可以说是非常简单了,但是我用的时候依旧碰到不少问题。现在总结出来,希望能让看到的各位少踩一些坑。

问题

原图片名为”Basler acA2040-120um (22196470)_20180521_135316631_0001“,而我需要将他们更改为“img_fx1_fy1_1”这样的格式.其中的数字是随着图片的序号而改变的。

基本代码

files = dir('*.bmp');% read all bmp files under the path
len=length(files);%read the bmp files' amount
for i=1:len
    oldname=files(i).name; %oldname
    newname=strcat('Img_', oldname); % newname
    command = ['RENAME' 32 , oldname,32 newname]; 
    % when a space is included in name, the '"'is inneed. 
    % 32 is equal to space in ASCII.
    % the oldname path can default but rebulit the path of newname is forbiden.
    % help rename in dos:
    %   RENAME [drive:][path]filename1 filename2
    %   REN [drive:][path]filename1 filrname2
    %   Note that you cannot specify a new drive or path for your
    %   destination file.
    status = dos(command);
    if status == 0
        disp([oldname, ' 已被重命名为 ', newname])
    else
        disp([oldname, ' 重命名失败!'])
    end
end

补充

newname

newname=strcat('Img_', oldname); % newname

这一句是在旧的文件名前加了“img_’字符,但是仅仅如此当然不会满足使用需求。

newname=strcat('Img_', oldname(20:end)); % newname

这一句相比上一句,多了一个对旧名字的选择性保留的功能,该例句保留了旧名称第20位开始到最末端的所有字符。

    oldname=files(i).name; %oldname
    fx = fix(i/80)+1;
    fy = mod(ceil(i/4)+19,20)+1;
    n = mod(i+3,4)+1;
    sfx = num2str(fx);
    sfy = num2str(fy);
    sn = num2str(n);
    ss = char('.bmp"');
    newname = strcat('"stripe',' fx',sfx,'_fy',sfy,'_',sn,ss);% newname

这一段则是对旧名字的完全重命名,该例子使用运算将图像名称按照每4张一小组,每20小组一大组规律命名。需要注意的是新的名字的格式是字符串,因此需要num2str将数字转换为字符串。另外如果需要规定编号的格式,如”0001”可以用

n=1;
fprintf('%04d',n);

实现。
还需要注意的一点是,如果名字里含有空格的话,需要用双引号将名字括起来,不然会报错。

DOS Command

    command = ['RENAME' 32 , oldname,32 newname]; 
    status = dos(command);

这一句相当于通过matlab调用dos系统命令,实现文件的重命名。在cmd中输入help rename来帮助我们了解输入的格式。

从上图可以看出,command = ['RENAME' 32 , oldname,32 newname];这句中的两个32实际是ASCII码,表示空格。在’rename‘和’filename1‘后分别有一个空格。
另外如果path没有写的话,系统会在默认的路径下寻找’filename1‘的文件,但是因为该默认路径一般不好找且并不是matlab该项目的存储路径或者之前读图片的路径,因此最好将完整路径填上。
仍需要注意的一点是,不管是旧文件名还是新文件名,若含有空格,必须使用双引号将其括住,否则会报错:”命令语法不正确“。

command = ['RENAME' 32 ,'"','C:\Users\10443\Desktop\img\',oldname,'"',32 newname]; 

如上例所示,双引号应该将路径和filename1扩在一起。

完整代码

至此,符合我个人需求的重命名代码已经改好,现将其附上。

%%% renname files
files = dir('C:\Users\10443\Desktop\img\*.bmp');% read all bmp files under the path
len=length(files);
for i=1:len
    oldname=files(i).name; %oldname
    fx = fix(i/80)+1;
    fy = mod(ceil(i/4)+19,20)+1;
    n = mod(i+3,4)+1;
    sfx = num2str(fx);
    sfy = num2str(fy);
    sn = num2str(n);
    ss = char('.bmp"');
    newname = strcat('"stripe',' fx',sfx,'_fy',sfy,'_',sn,ss)% newname
    command = ['RENAME' 32 ,'"','C:\Users\10443\Desktop\img\',oldname,'"',32 newname]; 
    % when a space is included in name, the '"'is inneed. 
    % 32 is equal to space in ASCII.
    % the oldname path can default but rebulit the path of newname is forbiden.
    % help rename in dos:
    %   RENAME [drive:][path]filename1 filename2
    %   REN [drive:][path]filename1 filrname2
    %   Note that you cannot specify a new drive or path for your destination file.
    status = dos(command);
    if status == 0
        disp([oldname, ' 已被重命名为 ', newname])
    else
        disp([oldname, ' 重命名失败!'])
    end
end