我想要一个 Windows 批处理文件,它可以重命名在子目录中找到的所有文件。
原始文件名的例子是
Clarity.Always.2015.this.WEB.P10.pdf
.
我想要的结果是:
所以,我希望生成的文件名是
Clarity Always (2015).pdf
.
我试图修改我在这里看到的几个示例,但无济于事,包括以下变体:
for /f "tokens=1,2,* delims=." %%F in ('dir /b "*.*"') do ren "%%F.%%G" "file %%F name.%%G"
for /f "delims=*" %a in ('dir File*_EN.txt /b /s') do ren "%a" File*_ENU.pdf
@ECHO Off
SETLOCAL ENABLEDELAYEDEXPANSION
rem The following settings for the source directory and filename are names
rem that I use for testing and deliberately include names which include spaces to make sure
rem that the process works using such names. These will need to be changed to suit your situation.
SET "sourcedir=u:\your files"
FOR /f "delims=" %%e IN (
'dir /b /s /a-d "%sourcedir%\*" '
) DO (
SET "fullfilename=%%e"
SET "filename=%%~ne"
SET "fileext=%%~xe"
CALL :findnewname
)
GOTO :EOF
:findnewname
FOR /L %%y IN (1980,1,2025) DO (
IF "%filename%" neq "!filename:.%%y.=!" SET "newname=!filename:.%%y.=.:%%y:!"&FOR /f "tokens=1,2delims=:" %%b IN ("!newname:.= !") DO ECHO REN "%fullfilename%" "%%b(%%c)%fileext%"
)
GOTO :EOF
将文件名和扩展名保存在用户变量中。
测试名称中是否出现字符串
.year.
。如果是这样,请将 .year.
替换为 .:year:
(冒号不能出现在文件名中),然后将生成的字符串标记化,借此机会用空格替换 .
s,拿起碎片并将它们粘回去一起重命名(只需被 echo
ed 解除武装)
[修改为扫描子目录]