Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assign command output to variable in batch file

I'm trying to assign the output of a command to a variable - as in, I'm trying to set the current flash version to a variable. I know this is wrong, but this is what I've tried:

set var=reg query hklm\SOFTWARE\Macromedia\FlashPlayer\CurrentVersion> 

or

reg query hklm\SOFTWARE\Macromedia\FlashPlayer\CurrentVersion >> set var 

Yeah, as you can see I'm a bit lost. Any and all help is appreciated!

like image 734
clines Avatar asked Apr 24 '13 22:04

clines


People also ask

How do you execute a CMD and save the output to a variable?

To store the output of a command in a variable, you can use the shell command substitution feature in the forms below: variable_name=$(command) variable_name=$(command [option ...] arg1 arg2 ...) OR variable_name='command' variable_name='command [option ...]

How do I assign a variable to the command line?

To set an environment variable, use the command " export varname=value ", which sets the variable and exports it to the global environment (available to other processes). Enclosed the value with double quotes if it contains spaces. To set a local variable, use the command " varname =value " (or " set varname =value ").


2 Answers

A method has already been devised, however this way you don't need a temp file.

for /f "delims=" %%i in ('command') do set output=%%i 

However, I'm sure this has its own exceptions and limitations.

like image 135
BDM Avatar answered Sep 21 '22 14:09

BDM


This post has a method to achieve this

from (zvrba) You can do it by redirecting the output to a file first. For example:

echo zz > bla.txt set /p VV=<bla.txt echo %VV% 
like image 24
Ian Kenney Avatar answered Sep 24 '22 14:09

Ian Kenney