【发布时间】:2021-04-27 17:42:03
【问题描述】:
我的文件夹中有一些文件,例如:
- Happysong.m4a.mp3
- 悲伤的歌曲 ft. H.E.R.m4a.mp3
我想将这些重命名为:
- Happysong.mp3
- 悲伤的歌曲 ft. H.E.R.mp3
这是我放在同一文件夹中的test.bat 文件的代码:
for /f "delims=" %%a in ('dir /b /a-d *.m4a.mp3') do (
set "oldName=%%~a"
set "newName=%oldName:.m4a=%"
ren %%a "%newName%"
)
它返回The syntax of the command is incorrect.
我试过了:
for /f "delims=" %%a in ('dir /b /a-d *.m4a.mp3') do (
set "oldName=%%~a"
set "newName=%oldName:.m4a=%"
ren %%a %newName%
)
返回ft. was unexpected at this time.
我在这里做错了什么?
【问题讨论】:
-
两个问题:你需要引用文件名来正确处理带空格的文件名,delayed expansion 才能在循环中正确使用变量:
ren "%%a" "!newname!" -
@Um9vdAo 打开command prompt,运行
help并查看Windows Commands 的(不完整的)输出列表以及简要说明。使用/?运行命令,以使用您的Windows 语言输出帮助,例如for /?和dir /?和set /?和ren /?。您还可以阅读已链接的 Microsoft 文档页面或 SS64.com - A-Z index of Windows CMD commands 上的命令,其中还有 How-to 指南。 -
哇!非常感谢您提供的资源和说明!对此,我真的非常感激。我一定会调查这些! @Mofi
标签: windows batch-file cmd rename