无法将批处理脚本的参数正确传递到循环语句内的命令中
Can't pass correctly the parameters of batch script into a command inside a loop statement
我在将参数传递给脚本内的命令时遇到问题,该脚本将在线流下载到单个目录,然后使用 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"
)
你能帮我解决这个问题吗?
请打开 command prompt、运行 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
为了了解使用的命令及其工作原理,请打开 command prompt window,在其中执行以下命令,并仔细阅读为每个命令显示的所有帮助页面。
del /?
echo /?
endlocal /?
for /?
goto /?
if /?
md /?
pause /?
rd /?
rem /?
set /?
setlocal /?
timeout /?
另请参阅:
- 关于 Using command redirection operators
的 Microsoft 文章
- DosTips 论坛主题:ECHO. FAILS to give text or blank line - Instead use ECHO/
我在将参数传递给脚本内的命令时遇到问题,该脚本将在线流下载到单个目录,然后使用 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"
)
你能帮我解决这个问题吗?
请打开 command prompt、运行 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
为了了解使用的命令及其工作原理,请打开 command prompt window,在其中执行以下命令,并仔细阅读为每个命令显示的所有帮助页面。
del /?
echo /?
endlocal /?
for /?
goto /?
if /?
md /?
pause /?
rd /?
rem /?
set /?
setlocal /?
timeout /?
另请参阅:
- 关于 Using command redirection operators 的 Microsoft 文章
- DosTips 论坛主题:ECHO. FAILS to give text or blank line - Instead use ECHO/