Windows 批处理文件重命名部分文件名

Windows 批处理文件重命名部分文件名,windows,batch-file,cmd,Windows,Batch File,Cmd,我想用别的东西替换文件夹中所有文件名的一部分 123_abc_def.txt、234_abc1_def1.txt、333_abc2_edf2.txt 等等 应该是: 908_abc_def.txt, 908_abc1_def1.txt,908_abc2_edf2.txt 我尝试的是: echo on SET data=%~dp0 for /f "tokens=1,2,3 delims=_" %%a in ( 'dir /b /a-d "%data%\*_*.txt" ' ) Do ( ren

我想用别的东西替换文件夹中所有文件名的一部分

123_abc_def.txt、234_abc1_def1.txt、333_abc2_edf2.txt 等等

应该是: 908_abc_def.txt, 908_abc1_def1.txt,908_abc2_edf2.txt

我尝试的是:

echo on
SET data=%~dp0
for /f "tokens=1,2,3 delims=_" %%a in (
'dir /b /a-d "%data%\*_*.txt" '
) Do (

ren "%data%%%a_%%b_%%c" "%data%908_%%b_%%c"
)

但我总是遇到语法错误。我在这里做错了什么?

如果看起来不错,就把-whatif脱下来

powershell "dir | ren -newname { $_.name -replace '^...','908' } -whatif"

如果它看起来不错,就把它脱下来

powershell "dir | ren -newname { $_.name -replace '^...','908' } -whatif"

使用powershell使用此选项:

powershell -NoProfile -Command {Get-ChildItem "FilePath" | Rename-Item -NewName {$_.Name -replace "^\d{3}", "908"} 

使用powershell使用此选项:

powershell -NoProfile -Command {Get-ChildItem "FilePath" | Rename-Item -NewName {$_.Name -replace "^\d{3}", "908"} 

无法将文件重命名为目录路径,因此会出现语法错误。它应该是
ren”%data%%%a\u%%b\uC“908\u%%b\uC”
,目标应该是一个文件名。谢谢,我没有看到路径和文件名有错误。你不能将文件重命名为目录路径,这就是为什么你会出现语法错误。它应该是
ren”%data%%%a\u%%b\uC“908\u%%b\uC”
,目标应该是一个文件名。谢谢,我没有看到路径和文件名有错误,如果文件名以字母而不是数字开头,则可能是关键的。如果文件名以字母而不是数字开头,则可能是关键的。