0

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

aschipfl
  • 33,626
  • 12
  • 54
  • 99
manrilla
  • 31
  • 5

1 Answers1

1

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%
  • 4
    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 "location=%%A"` – Compo Jun 01 '17 at 15:36