2

Fairly new to .bat programming I wanted to make a script that does some things according to the installed python version. However, I came across my first problem, when it comes to storing the python version, my script looks like the following until now:

set version="python --version"
echo %version%

Just gives back:

"python --version"

So the question cuts down to: How to save a a result from a comamnd line command into a variable in bat.file?

I only found this solution:

FOR /F "tokens=* USEBACKQ" %%F IN (`python --version`) DO (
SET var=%%F
)
ECHO %var%

By this question: How to set commands output as a variable in a batch file - which seems too much, if I compare it a bash file - so is there a more elegant way to store the results?

Kev1n91
  • 3,553
  • 8
  • 46
  • 96
  • 1
    If you want elegance, use a real scripting language, e.g., cscript or Powershell. Batch language dates from DOS, was intended only for trivial tasks, and was designed for non-programmers. – Harry Johnston Aug 23 '17 at 08:19
  • 1
    There is no other way as shown in the linked thread: either a `for /F` loop, or redirection to and from a temporary file. Note that the `for /F` approach assigns the last line of the output to the variable, while the other one assigns the first line. – aschipfl Aug 23 '17 at 08:19

0 Answers0