既然你说你使用的是Windows 7,我知道你可以使用PowerShell。下面是一个PowerShell脚本:
$re = '^(\d+)_((?:(?:[a-zA-Z0-9]+)_?)+)_(\d{9})-(\d{4}-\d\d-\d\d)\.(\w+)$'
dir | ForEach-Object {$_ -replace $re, 'convert "$1" -annotate "$2" -annotate2 "$3" -annotate3 "-$4"'}
根据您在问题中提供的文件名
7_D_D1_012345678-2015-07-07.pdf
8_A_087654321-2015-07-07.pdf
10_D_D1_011122558-2015-07-07.pdf
100_C_CCC1_C2_C3_C4_055555555-2015-07-07.pdf
它会生成以下文本输出:
convert "100" -annotate "C_CCC1_C2_C3_C4" -annotate2 "055555555" -annotate4 "2015-07-07"
convert "10" -annotate "D_D1" -annotate2 "011122558" -annotate4 "2015-07-07"
convert "7" -annotate "D_D1" -annotate2 "012345678" -annotate4 "2015-07-07"
convert "8" -annotate "A" -annotate2 "087654321" -annotate4 "2015-07-07"
首先按文件名排序,因此以100开头的文件排在第一位,以8开头的文件排在最后。
通过将此文本输出重定向到.cmd文件中,您可以根据需要执行转换命令。
以下是该正则表达式的详细说明:
Beginning of line or string
[1]: A numbered capture group. [\d+]
Any digit, one or more repetitions
_
[2]: A numbered capture group. [(?:(?:[a-zA-Z0-9]+)_?)+]
Match expression but don't capture it. [(?:[a-zA-Z0-9]+)_?], one or more repetitions
(?:[a-zA-Z0-9]+)_?
Match expression but don't capture it. [[a-zA-Z0-9]+]
Any character in this class: [a-zA-Z0-9], one or more repetitions
_, zero or one repetitions
_
[3]: A numbered capture group. [\d{9}]
Any digit, exactly 9 repetitions
-
[4]: A numbered capture group. [\d{4}-\d\d-\d\d]
\d{4}-\d\d-\d\d
Any digit, exactly 4 repetitions
dir folder >tmp.cmd
,然后使用具有正则表达式替换功能的文本编辑器(例如vim,emacs,Notepad Plus)将文件名转换为所需的命令。 - Ross Pressersed
(或awk
,或perl
)可以解决问题。你有什么可用的工具?你愿意下载一些工具吗,还是必须直接使用批处理?那PowerShell呢? - Ross Presser