usr83
usr83

Reputation: 1

WIN cmd redirection operators prevent duplicate lines?

When outputting to a file how do i prevent duplicate lines?

ffmpeg.exe filename.mp4 -o filename.mkv >> output.log

The .log file will be filled with 10+ lines of progress: 0% and so on for each step of encoding

How do i prevent this so it will only write progress: 0% once then wait for progress 1% and so on?

Upvotes: 0

Views: 35

Answers (1)

Mofi
Mofi

Reputation: 49097

Here is a batch code where FOR reads each line from STDOUT and STDERR of the executed command and compares it with previous read line. If the newly read line is different to previously read line, this line is output to the file using redirection operator >>.

@echo off
setlocal EnableExtensions EnableDelayedExpansion
del output.log 2>nul
set "Line="
for /F "delims=" %%I in ('ffmpeg.exe filename.mp4 -o filename.mkv 2^>^&1') do (
    if not "!Line!" == "%%I" (
        set "Line=%%I"
        echo !Line!>>output.log
    )
)
endlocal

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

  • del /?
  • echo /?
  • endlocal /?
  • for /?
  • if /?
  • set /?
  • setlocal /?

Read also the Microsoft article about Using Command Redirection Operators.

PS: I don't have ffmpeg installed and therefore could not test above nor do I have the documentation for this tool installed. But take a look at ffmpeg output parse in batch script because I think all you really need is redirecting messages written to STDERR into the output file with:

ffmpeg.exe filename.mp4 -o filename.mkv 2>>output.log

Upvotes: 1

Related Questions