【问题标题】:Why does this REN command return "The syntax of the command is incorrect"?为什么这个 REN 命令返回“命令的语法不正确”?
【发布时间】:2021-04-27 17:42:03
【问题描述】:

我的文件夹中有一些文件,例如:

  1. Happysong.m4a.mp3
  2. 悲伤的歌曲 ft. H.E.R.m4a.mp3

我想将这些重命名为:

  1. Happysong.mp3
  2. 悲伤的歌曲 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


【解决方案1】:

可能会这样做:

For %G In (*.m4a.mp3) Do @For %H In ("%~nG") Do @Ren "%G" "%~nH%~xG"

或者像这样来自

@For %%G In (*.m4a.mp3) Do @For %%H In ("%%~nG") Do @Ren "%%G" "%%~nH%%~xG"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-06-10
    • 2019-03-22
    • 1970-01-01
    • 2012-05-11
    • 1970-01-01
    • 2013-01-28
    • 1970-01-01
    相关资源
    最近更新 更多