Assign command output to variable in batch file

Batch FileVariablesFlashCmdRegistry

Batch File Problem Overview


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!

Batch File Solutions


Solution 1 - Batch File

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.

Solution 2 - Batch File

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%

Solution 3 - Batch File

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

http://ss64.com/nt/for_f.html

PS: Change the reg key used if needed.

Solution 4 - Batch File

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:

  1. the output from the command executed via call :EXEC command is stored in %output%. Batch cmd doesn't cares about scopes so %output% will be also available in the main script.
  2. the @ the beginning is just decoration and there to suppress echoing the command line. You may delete them all and just put some @echo off at the first line is really dislike that. However like this I find debugging much more nice. Decoration Number two is prompt -$G. It's there to make command prompt look like this ->
  3. I use :: instead of rem
  4. the tilde(~) in %~1 is to remove quotes from the first argument
  5. 2^>nul is there to suppress/discard stderr error output. Normally you would do it via 2>nul. Well the ^ the batch escape char is there avoids to early resolving the redirector(>). There's some simulare use a little later in the script: echo %1 -^>... so there ^ makes it possible the output a '>' via echo what else wouldn't have been possible.
  6. even if the compare at @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. ;)

Solution 5 - Batch File

This is work for me

@FOR /f "delims=" %i in ('reg query hklm\SOFTWARE\Macromedia\FlashPlayer\CurrentVersion') DO set var=%i
echo %var%

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionclinesView Question on Stackoverflow
Solution 1 - Batch FileBDMView Answer on Stackoverflow
Solution 2 - Batch FileIan KenneyView Answer on Stackoverflow
Solution 3 - Batch FileElektroStudiosView Answer on Stackoverflow
Solution 4 - Batch FileNaduView Answer on Stackoverflow
Solution 5 - Batch FileeQ19View Answer on Stackoverflow