Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Batch - Can't print spaces beginning of a string

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!

like image 669
ozcanovunc Avatar asked Jun 24 '15 11:06

ozcanovunc


Video Answer


1 Answers

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

like image 99
Melebius Avatar answered Oct 12 '22 12:10

Melebius