Batch file Can';t将批处理脚本的参数正确传递到循环语句内的命令中

Batch file Can';t将批处理脚本的参数正确传递到循环语句内的命令中,batch-file,cmd,windows-scripting,Batch File,Cmd,Windows Scripting,我在向脚本中的命令传递参数时遇到问题,脚本将在线流下载到单个目录,然后使用ffmpeg修复错误 首先,我检查目录是否存在,如果不存在,则创建一个: if exist %1 ( echo Directory exists ) ELSE ( mkdir "%1%" echo Directory created ) 然后是一个主循环,它尝试下载流并修复其中的错误 for /L %%C in (1,1,10000) do ( streaml

我在向脚本中的命令传递参数时遇到问题,脚本将在线流下载到单个目录,然后使用ffmpeg修复错误

首先,我检查目录是否存在,如果不存在,则创建一个:

if exist %1 (
      echo Directory exists
   ) ELSE (
      mkdir "%1%"
      echo Directory created
   )
然后是一个主循环,它尝试下载流并修复其中的错误

for /L %%C in (1,1,10000) do (

      streamlink -o "%%1\%%1%%C.mp4" "some.url/%2" best   
      if exist %1\%1%C.mp4 (
          d:\streamlink\ffmpeg\bin\ffmpeg.exe -loglevel debug -i "%1\%1%C.mp4" -c:v copy -c:a copy "%1\%1%C_o.mp4" 1>"%1%\log\%1%%C.log" 2>"%1%\err\%1%%C.err"
         )
      timeout /T 300 
    )
例如,如果我执行:

script.cmd foo xyz
然后,应在第一个循环中执行:

streamlink -o "foo\foo1.mp4" "some.url/xyz" best   
      if exist foo\foo1.mp4 (
          d:\streamlink\ffmpeg\bin\ffmpeg.exe -loglevel debug -i "foo\foo1.mp4" -c:v copy -c:a copy "foo\foo1_o.mp4" 1>"foo\log\foo1.log" 2>"foo\err\foo1.err"
         )
您能帮我解决这个问题吗?

请打开,运行
call/?
并阅读输出帮助,解释如何从批处理文件中引用批处理文件的参数。参数0是当前由
cmd.exe
处理的批处理文件

我建议将此批处理文件用于任务,尽管我不知道for循环的真正作用

@echo off
setlocal EnableExtensions DisableDelayedExpansion

if "%~1" == "" (
    echo ERROR: %~nx0 must be called with a directory path as first argument.
    goto EndBatch
)

if "%~2" == "" (
    echo ERROR: %~nx0 must be called with ??? as second argument.
    goto EndBatch
)

rem Assign the directory path to an environment variable.
set "DirectoryPath=%~1"
rem Make sure the directory path contains \ and not / as directory separator.
set "DirectoryPath=%DirectoryPath:/=\%"
rem Make sure the directory path ends with a backslash.
if not "%DirectoryPath:~-1%" == "\" set "DirectoryPath=%DirectoryPath%\"

rem Check if the directory exists already and create it otherwise.
if exist "%DirectoryPath%" (
      echo Directory exists.
) else (
    md "%DirectoryPath%"
    if exist "%DirectoryPath%" (
        echo Directory created.
    ) else (
        echo ERROR: %~nx0 failed to create directory "%DirectoryPath:~0,-1%"
        goto EndBatch
    )
)

rem Get name of last directory from directory path for the various file names.
for %%I in ("%DirectoryPath:~0,-1%") do (
    set "VideoFileName=%DirectoryPath%%%~nxI"
    set "StdLogFileName=%DirectoryPath%log\%%~nxI
    set "ErrLogFileName=%DirectoryPath%err\%%~nxI
)

rem Create the two additional subdirectories with suppressing the
rem error messages output on these directories existing already.
md "%DirectoryPath%log" 2>nul
md "%DirectoryPath%err" 2>nul

for /L %%I in (1,1,10000) do (
    streamlink.exe -o "%VideoFileName%%%I.mp4" "some.url/%~2" best
    if not errorlevel 1 if exist "%VideoFileName%%%I.mp4" (
        "D:\streamlink\ffmpeg\bin\ffmpeg.exe" -loglevel debug -i "%VideoFileName%%%I.mp4" -c:v copy -c:a copy "%VideoFileName%%%I_o.mp4" >"%StdLogFileName%%%I.log" 2>"%ErrLogFileName%%%I.log"
    )
    rem Don't know what this wait is for.
    %SystemRoot%\Sytem32\timeout.exe /T 300
)

rem Remove all empty standard log files.
for %%I in ("%StdLogFileName%*.log") do if %%~zI == 0 del "%%I"
rem Remove the standard log file directory on being empty now.
rd "%DirectoryPath%log" 2>nul

rem Remove all empty error log files.
for %%I in ("%ErrLogFileName%*.log") do if %%~zI == 0 del "%%I"
rem Remove the error log file directory on being empty now.
rd "%DirectoryPath%err" 2>nul

:EndBatch
echo/
endlocal
pause
要了解所使用的命令及其工作方式,请打开一个窗口,在其中执行以下命令,并非常仔细地阅读为每个命令显示的所有帮助页面

  • del/?
  • echo/?
  • endlocal/?
  • 获取/?
  • goto/?
  • 如果/?
  • md/?
  • 暂停/?
  • rd/?
  • rem/?
  • 设置/?
  • setlocal/?
  • 超时/?
另见:

  • 关于Microsoft的文章
  • DosTips论坛主题:
请打开,运行
call/?
并阅读输出帮助,解释如何从批处理文件中引用批处理文件的参数。参数0是当前由
cmd.exe
处理的批处理文件

我建议将此批处理文件用于任务,尽管我不知道for循环的真正作用

@echo off
setlocal EnableExtensions DisableDelayedExpansion

if "%~1" == "" (
    echo ERROR: %~nx0 must be called with a directory path as first argument.
    goto EndBatch
)

if "%~2" == "" (
    echo ERROR: %~nx0 must be called with ??? as second argument.
    goto EndBatch
)

rem Assign the directory path to an environment variable.
set "DirectoryPath=%~1"
rem Make sure the directory path contains \ and not / as directory separator.
set "DirectoryPath=%DirectoryPath:/=\%"
rem Make sure the directory path ends with a backslash.
if not "%DirectoryPath:~-1%" == "\" set "DirectoryPath=%DirectoryPath%\"

rem Check if the directory exists already and create it otherwise.
if exist "%DirectoryPath%" (
      echo Directory exists.
) else (
    md "%DirectoryPath%"
    if exist "%DirectoryPath%" (
        echo Directory created.
    ) else (
        echo ERROR: %~nx0 failed to create directory "%DirectoryPath:~0,-1%"
        goto EndBatch
    )
)

rem Get name of last directory from directory path for the various file names.
for %%I in ("%DirectoryPath:~0,-1%") do (
    set "VideoFileName=%DirectoryPath%%%~nxI"
    set "StdLogFileName=%DirectoryPath%log\%%~nxI
    set "ErrLogFileName=%DirectoryPath%err\%%~nxI
)

rem Create the two additional subdirectories with suppressing the
rem error messages output on these directories existing already.
md "%DirectoryPath%log" 2>nul
md "%DirectoryPath%err" 2>nul

for /L %%I in (1,1,10000) do (
    streamlink.exe -o "%VideoFileName%%%I.mp4" "some.url/%~2" best
    if not errorlevel 1 if exist "%VideoFileName%%%I.mp4" (
        "D:\streamlink\ffmpeg\bin\ffmpeg.exe" -loglevel debug -i "%VideoFileName%%%I.mp4" -c:v copy -c:a copy "%VideoFileName%%%I_o.mp4" >"%StdLogFileName%%%I.log" 2>"%ErrLogFileName%%%I.log"
    )
    rem Don't know what this wait is for.
    %SystemRoot%\Sytem32\timeout.exe /T 300
)

rem Remove all empty standard log files.
for %%I in ("%StdLogFileName%*.log") do if %%~zI == 0 del "%%I"
rem Remove the standard log file directory on being empty now.
rd "%DirectoryPath%log" 2>nul

rem Remove all empty error log files.
for %%I in ("%ErrLogFileName%*.log") do if %%~zI == 0 del "%%I"
rem Remove the error log file directory on being empty now.
rd "%DirectoryPath%err" 2>nul

:EndBatch
echo/
endlocal
pause
要了解所使用的命令及其工作方式,请打开一个窗口,在其中执行以下命令,并非常仔细地阅读为每个命令显示的所有帮助页面

  • del/?
  • echo/?
  • endlocal/?
  • 获取/?
  • goto/?
  • 如果/?
  • md/?
  • 暂停/?
  • rd/?
  • rem/?
  • 设置/?
  • setlocal/?
  • 超时/?
另见:

  • 关于Microsoft的文章
  • DosTips论坛主题:

您的
[%]
键是否粘在键盘上?或者有一些自动化软件偶尔添加这些字符吗?没有,我尝试了几乎所有已知的方法来使用这些[%]字符使脚本按我所希望的方式工作,在这里我粘贴了我最后一次尝试,找出了仍然不起作用的方法。你是如何用三种不同的方式键入
%1\%C
的<代码>%1\%%C是唯一正确的方法。您的
[%]
键是否粘在键盘上?或者有一些自动化软件偶尔添加这些字符吗?没有,我尝试了几乎所有已知的方法来使用这些[%]字符使脚本按我所希望的方式工作,在这里我粘贴了我最后一次尝试,找出了仍然不起作用的方法。你是如何用三种不同的方式键入
%1\%C
的<代码>%1\%C是唯一正确的方法。