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!
This question is related to
batch-file
variables
flash
cmd
registry
You can't assign a process output directly into a var, you need to parse the output with a For /F loop:
@Echo OFF
FOR /F "Tokens=2,*" %%A IN (
'Reg Query "HKEY_CURRENT_USER\Software\Macromedia\FlashPlayer" /v "CurrentVersion"'
) DO (
REM Set "Version=%%B"
Echo Version: %%B
)
Pause&Exit
PS: Change the reg key used if needed.
Okay here some more complex sample for the use of For /F
:: Main
@prompt -$G
call :REGQUERY "Software\Classes\CLSID\{3E6AE265-3382-A429-56D1-BB2B4D1D}"
@goto :EOF
:REGQUERY
:: Checks HKEY_LOCAL_MACHINE\ and HKEY_CURRENT_USER\
:: for the key and lists its content
@call :EXEC "REG QUERY HKCU\%~1"
@call :EXEC "REG QUERY "HKLM\%~1""
@goto :EOF
:EXEC
@set output=
@for /F "delims=" %%i in ('%~1 2^>nul') do @(
set output=%%i
)
@if not "%output%"=="" (
echo %1 -^> %output%
)
@goto :EOF
I packed it into the sub function :EXEC so all of its nasty details of implementation doesn't litters the main script. So it got some kinda some batch tutorial. Notes 'bout the code:
echo %1 -^>...
so there ^ makes it possible the output a '>' via echo what else wouldn't have been possible.@if not "%output%"==""
looks like in most common programming languages - it's maybe different that you expected (if you're not used to MS-batch). Well remove the '@' at the beginning. Study the output. Change it tonot %output%==""
-rerun and consider why this doesn't work. ;)This is work for me
@FOR /f "delims=" %i in ('reg query hklm\SOFTWARE\Macromedia\FlashPlayer\CurrentVersion') DO set var=%i
echo %var%
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%
Source: Stackoverflow.com