3

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.
5
  • what does for /f "tokens=1 delims=" %%a in ('compare -metric NCC "a.jpg" "b.jpg" "c.jpg"') do echo test %%a say? Commented Jun 23, 2015 at 19:41
  • It says 1ECHO is off but that means result variable hasn't been initialized right? Commented Jun 23, 2015 at 19:43
  • only one output line? I hoped there were two of them. Commented Jun 23, 2015 at 20:01
  • Yeah. Only one line. link Commented Jun 23, 2015 at 20:05
  • Sorry I tested the wrong cote btw. Your code outputs 1 only. But when I delete the test %%a part, the output stays same which is interesting. Commented Jun 23, 2015 at 20:29

2 Answers 2

2

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
Sign up to request clarification or add additional context in comments.

2 Comments

That worked but why do we need <nul,what is the aim of using it? (I am new to batch programming btw)
set /p is intended to ask the user for input. We mis-use it for writing a line without linebreak; to simulate an empty user input, we redirect the NUL device to it. Try set /p "x=Give me Input: " You'll notice, that your input keeps on the same line, just after the "give me input" prompt. It's a bit like cheating, but works well and is commonly used.
2

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"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.