Toni
Toni

Reputation: 1

how to assign the result of a batch command to a variable

If on the command line I execute:

c:\digitemp.exe -t0 -o%C -q  > res1.txt

res1.txt contains correctly the numerical temperature in Celsius (say: 24.23456). But if the same command is executed inside a bat file (say: test.bat):

@ECHO OFF
ECHO Hola pootol!
ECHO.
c:\digitemp.exe -t0 -o%C -q  > res1.txt 
rem set pootol = < res1.txt
rem set pootol
ECHO Prem una tecla per sortir.
pause > null

res1.txt contains a wrong Celsius value that I suspect is related to the argument " -o%C ". As you can see I rem the variable assing cause pootol var is wrong assigned with the Celsius value before it is mentioned. What am I doing wrong?

Upvotes: 0

Views: 528

Answers (2)

jeb
jeb

Reputation: 82410

The problem in your case is the % sign, as it's evaluated different in the cmd-line and in batch files.
In batch files you can escape it with doubling it.

So your code looks like

c:\digitemp.exe -t0 -o%%C -q  > res1.txt

Upvotes: 2

shf301
shf301

Reputation: 31404

In batch files % is used to denote variables. So %C is interpreted inside the batch file as a variable and replaced with its value. Since it doesn't have a value it is replaced with an empty string.

Use the caret ^ character to escape the % so that the interpreter treats the % as a normal character.

c:\digitemp.exe -t0 -o^%C -q  > res1.txt 

Upvotes: 0

Related Questions