【问题标题】:Looping through directories containing files with special characters in file name results in errors [duplicate]遍历包含文件名中具有特殊字符的文件的目录会导致错误[重复]
【发布时间】:2021-12-07 13:52:51
【问题描述】:

我在一个文件夹中有一堆音频文件(类型 opus、m4a、webm、mp3)和缩略图文件(jpg、webp)。大多数音频文件都有相应的缩略图(相同的名称,不同的文件扩展名)。我想要做的是遍历该目录并将音频文件转换为 mp3 格式(如果还没有)并合并到相应的缩略图中(如果存在)。 我的问题:一些文件名包含特殊字符,如括号,这就是我的脚本中断的地方。一直在说

) was unexpected at this time.

所以我的问题是:如何正确转义所有这些文件名,使其不再干扰?

我的代码:

@echo off
setlocal EnableDelayedExpansion
set "FFMPEG=C:\ffmpeg\bin\ffmpeg.exe"
for /f "delims=" %%i in ('dir /b') do (
  if "%%i"=="convert.cmd" ( 
    goto error1
  )
  if "%%~xi"==".jpg" (
    goto error2
  )
  if "%%~xi"==".webp" (
    goto error2
  )
  if "%%~xi"==".mp3" (
    goto error3
  )

  if exist "%%~ni".jpg (
    "%FFMPEG%" -i "%%i" -i "%%~ni".jpg -y -map_metadata 0 -map 0 -map 1 -movflags use_metadata_tags -vcodec jpeg2000 -acodec libmp3lame ..\output\\"%%~ni".mp3 
    del "%%~ni".jpg
    del "%%i"
    goto continue
  )

  if exist "%%~ni".webp (
    "%FFMPEG%" -i "%%i" -i "%%~ni".webp -y -map_metadata 0 -map 0 -map 1 -movflags use_metadata_tags -vcodec jpeg2000 -acodec libmp3lame ..\output\\"%%~ni".mp3 
    del "%%~ni".webp
    del "%%i"
    goto continue
  )

  "%FFMPEG%" -i "%%i" -y -map_metadata 0 -map 0 -movflags use_metadata_tags -vcodec jpeg2000 -acodec libmp3lame ..\output\\"%%~ni".mp3 
  del "%%i"
  goto continue

  :error1
  echo Ignore script file
  goto continue

  :error2
  echo File doesn't contain an audio stream
  goto continue

  :error3
  echo File is already in mp3 format
  goto continue

  :continue
)
endlocal

【问题讨论】:

  • 你不能在 for 循环 do 块内有标签。
  • 感谢您的回答,我稍后会尝试。
  • 也要使用正确的引用。 if exist "%%~ni.jpg""%FFMPEG%" -i "%%i" -i "%%~ni.webp"。使用所有文件名和文件路径引用更改为这种引用样式。引号应始终围绕整个文件和文件夹路径。不仅仅是变量。
  • 如果您只指定要处理的文件,则可以摆脱大多数IF 命令。 for /f "delims=" %%i in ('dir /b *.m4a *.webm') do
  • 这个答案帮助我理解了这个问题。 stackoverflow.com/a/8481978/17074368

标签: for-loop batch-file cmd escaping


【解决方案1】:

这段代码现在对我有用:

@echo off
setlocal EnableExtensions DisableDelayedExpansion
set "FFMPEG=C:\ffmpeg\bin\ffmpeg.exe"
for /f "delims=" %%i in ('dir /b') do (
  if /I not "%%i"=="%~nx0" if /I not "%%~xi"==".jpg" if /I not "%%~xi"==".webp" if /I not "%%~xi"==".mp3" ( 
    if exist "%%~ni.jpg" (
      "%FFMPEG%" -i "%%i" -i "%%~ni.jpg" -y -map_metadata 0 -map 0 -map 1 -movflags use_metadata_tags -acodec libmp3lame -f mp3 ..\output\\"%%~ni.mp3"
      del "%%~ni.jpg"
      del "%%i"
    ) else if exist "%%~ni.webp" (
      "%FFMPEG%" -i "%%i" -i "%%~ni.webp" -y -map_metadata 0 -map 0 -map 1 -movflags use_metadata_tags -acodec libmp3lame -f mp3 ..\output\\"%%~ni.mp3" 
      del "%%~ni.webp"
      del "%%i"
    ) else (
      "%FFMPEG%" -i "%%i" -y -map_metadata 0 -map 0 -movflags use_metadata_tags -acodec libmp3lame ..\output\\"%%~ni.mp3" 
      del "%%i"
    )
  )
)
endlocal

事实上,问题不是由括号引起的。正如@Compo 在上面的评论中所说,在 for 循环中使用“goto”是不可能的。

【讨论】:

    猜你喜欢
    • 2015-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-12
    相关资源
    最近更新 更多