Command output to a file with batch scripting

108 Views Asked by At

I am trying to generate an xml file. I compare two images using a command which returns a number. But when I try to redirect its output to a file, it prints the number with a newline character.

        echo a.jpg >> "result.txt"
        compare -metric NCC "a.jpg" "b.jpg" "c.jpg" 2>> "result.txt"

Expected output like:

        a.jpg 1

But it outputs:

        a.jpg
        1

I tried to get the result from the command and tried to concatenate with the a.jpg but I couldn't have managed.

        for /f "tokens=1 delims=" %%a in ('compare -metric NCC "a.jpg" "b.jpg" "c.jpg"') do set result=%%a
        echo %result% 
        REM outputs 1ECHO is off.
2

There are 2 best solutions below

2
On BEST ANSWER

now I know, what happens:

compare -metric NCC "a.jpg" "b.jpg" "c.jpg" 2>> "result.txt"

your desired output is on STDERR, not on STDOUT (very unusual). But for captures STDOUT only.

It should be possible do adapt the forconstruct, but it's simpler to use:

<nul set /p "=a.jpg " >> "result.txt"
REM this line writes a string without linefeed

compare -metric NCC "a.jpg" "b.jpg" "c.jpg" 2>> "result.txt"
REM this line appends the STDERR of the "compare" command to the line
0
On

First command adds a newline. Use it like this to avoid it and get output in one line.

echo|set /p=a.jpg >> "result.txt"