6

I have a command called compare which returns a number. I want to print some spaces and then "text" then the number to a file. Expected output is like:

    text1

My code prints text1 with no spaces

set "output=     text"
<nul set /p "=!output!" >> "%resultFile%"
compare -metric NCC "a.jpg" "b.jpg" "c.jpg" 2>> "%resultFile%"

I tried to print a tab character echo <TAB> >> "%resultFile%" but it gave me an error ">> was unexpected at this time." What should I do? Thanks in advance!

ozcanovunc
  • 703
  • 1
  • 8
  • 29

1 Answers1

1

This works well:

set "output=     text"
>> "%resultFile%" echo %output%

So what about appending the command output in the batch itself?

set "output=     text"
call compare -metric NCC "a.jpg" "b.jpg" "c.jpg" 2> temp.txt
set /p number=<temp.txt
del temp.txt
>> "%resultFile%" echo %output%%number%

See https://stackoverflow.com/a/2340018/711006. I’d prefer the for option but it does not work for the error output.

EDIT

The OP suggested editing the last line to this:

echo !output!!number! >> "%resultFile%"

The !’s instead of %’s are needed when the commands are expanded together, for example as a part of an if or for block. However, one has to issue the command setlocal EnableDelayedExpansion before and we have not mentioned this in this question.

And I would recommend further reading about swapping the command and output redirection: Problems with an extra space when redirecting variables into a text file

Community
  • 1
  • 1
Melebius
  • 6,183
  • 4
  • 39
  • 52
  • @ozcanovunc Please paste suggestions on code changes into comments next time, so we can discuss the change together and everyone can follow our thoughts. – Melebius Jun 24 '15 at 13:25