batch-filecmdwindows-scripting

Can't pass correctly the parameters of batch script into a command inside a loop statement


I have a problem with passing parameters to commands inside the script which downloads online stream to individual directory and then fixes errors using ffmpeg.

First I check if directory exists and if not then create one:

if exist %1 (
      echo Directory exists
   ) ELSE (
      mkdir "%1%"
      echo Directory created
   )

And then there is a main loop which tries to download the stream and fix errors in it.

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 
    )

So for example if I execute:

script.cmd foo xyz

then in first loop should be executed:

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"
         )

Could you help me with this?


Solution

  • Please open a command prompt, run call /? and read the output help explaining how arguments of a batch file can be referenced from within a batch file. Argument 0 is the batch file currently processed by cmd.exe.

    I suggest this batch file for the task although not knowing what the FOR loop really does.

    @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
    

    To understand the commands used and how they work, open a command prompt window, execute there the following commands, and read the displayed help pages for each command, entirely and carefully.

    See also: