我想重命名某些目录中的文件,到目前为止我一直在使用这个:
@echo off
Setlocal enabledelayedexpansion
Set "Pattern= "
Set "Replace=_"
For %%a in (*.pdf,*.docx,*.xlsx,*.csv,*.doc,*.jpg,*.png,*.xlsm,*.ppt,*pptx) Do (
Set "File=%%~a"
Ren "%%a" "!File:%Pattern%=%Replace%!"
)
Exit
我想要多个包含空格和连字符的“模式”来“替换”。如何在“模式=”中添加额外的“下划线”?
答案1
你可以为一个变量分配 1 个值。因此,我将创建另一个名为 pattern2 的变量,并在最后使用 pattern2 传递另一个重命名命令。像这样
@echo off
Setlocal enabledelayedexpansion
Set "Pattern= "
Set "Pattern2=-"
Set "Pattern3=_-"
Set "Pattern4=-_"
Set "Pattern5=- "
Set "Pattern6= -"
Set "Pattern7=__"
Set "Replace=_"
For %%a in (*.pdf,*.docx,*.xlsx,*.csv,*.doc,*.jpg,*.png,*.xlsm,*.ppt,*pptx) Do (
Set "File=%%~a"
Ren "%%a" "!File:%Pattern%=%Replace%!"
Ren "%%a" "!File:%Pattern2%=%Replace%!"
Ren "%%a" "!File:%Pattern3%=%Replace%!"
Ren "%%a" "!File:%Pattern4%=%Replace%!"
Ren "%%a" "!File:%Pattern5%=%Replace%!"
Ren "%%a" "!File:%Pattern6%=%Replace%!"
Ren "%%a" "!File:%Pattern7%=%Replace%!"
)
Exit