Batch Modify Files and Folders Name in MATLAB

Oct. 06, 2022

Batch Modify Files Name

今天在整理电脑文件的时候遇到了这样一类问题,比如说现在有这样几个相同类型的文件:

1
2
3
4
5
PeakyBlinders01.jpg
PeakyBlinders02.jpg
PeakyBlinders03.jpg
PeakyBlinders04.jpg
...

我想要把PeakyBlinders01修改成01 Peaky Blinders

需要做的工作是提取文件名中的序号,然后保留序号,把文件名中的其他字符更改为其他的格式。在MATLAB中借助正则表达式,可以做到这一点:

1
2
3
4
5
6
7
8
9
clc, clear, close all

file = dir("*.jpg");
for i = 1:numel(file)
    oldname = file(i).name;
    matchStr = regexp(oldname, '[0-9][0-9]', "match");
    newname = [num2str(matchStr{1}), ' Peaky Blinders.jpg'];
    eval(['!rename', ' "', oldname, '" "', newname, '"']);
end

运行代码后,可以看到文件名被修改成预期的样子:

image-20221006223019726

代码中:

  • matchStr = regexp(oldname, '[0-9][0-9]', "match");,表示使用正则表达式匹配字符串中连续的两个数字;
  • eval(['!rename', ' "', oldname, '" "', newname, '"']);,其中!rename表示调用Windows系统的rename函数修改文件名,注意不要忘掉空格和双引号。


Batch Modify Folders Name

Added on Oct. 26, 2023.

Today, I want to change the folders name in my website post assets repository [2] from <year>.<month>. <date> <foldername>:

to <year>-<month>-<date> <foldername>. Similarly, I wrote a simple script to realise it:

1
2
3
4
5
6
7
8
9
10
11
12
13
clc,clear,close all

folderInfo = dir(pwd);
pat = digitsPattern(4)+"."+digitsPattern(2)+"."+digitsPattern(2);

for i = 3:numel(folderInfo)
    oldDate = extract(folderInfo(i).name,pat);
    if ~isempty(oldDate)
        oldDate = oldDate{1};
        newName = replace(folderInfo(i).name,oldDate,sprintf("%s-%s-%s",oldDate(1:4),oldDate(6:7),oldDate(9:10)));
        eval(['!rename', ' "', folderInfo(i).name, '" "', newName, '"']);
    end
end

The results are like:

image-20231026120413011


Reference

[1] regexp - MathWorks.

[2] HelloWorld-1017/HelloWorld-1017.github.io-post-assets.

[3] digitsPattern: Match digit characters. - MathWorks.