manrilla
manrilla

Reputation: 31

Outputting Batch Command to Variable

I do not have a strong background in batch scripts, so apologies if there is an obvious answer.

I am trying to direct the output of a command prompt to a variable. I can output it to a file like this

go test -race > output.txt

In this case, output.txt gets populated with the output of go test -race. What I am trying to do is output it to a variable, something along the lines of

set iamavariable=nothotdog
go test -race > %iamavariable%
echo %location%

This creates a file called nothotdog that has the output of go test -race, and echoes nothotdog, instead of echoing the output of go test -race

Upvotes: 0

Views: 939

Answers (1)

user6250760
user6250760

Reputation:

To correctly get the result of a command, you need a for loop.

for /f %%p in ('your command') do set result=%%p
echo %result%

If the command was outputting more than a single word/number/character/digit then perhaps this would be better:

For /F "Delims=" %%A In ('go test -race') Do Set "result=%%A"
Echo %result%

Upvotes: 1

Related Questions