Outputting a command to a batch script file

I am trying to create an xml file. I am comparing two images using a command that returns a number. But when I try to redirect my 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 Result:

        a.jpg 1

      

But it outputs:

        a.jpg
        1

      

I tried to get the output from the command and tried to merge with a.jpg but I could not work it out.

        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.

      

+3


source to share


2 answers


Now I know what's going on:

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

      

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

only STDOUT commits.



The construct may need to be adapted for

, but it's easier 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

      

+2


source


The first command adds a new line. Use it in such a way as to avoid this and get the output on one line.



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

      

+2


source







All Articles