53

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!

phuclv
  • 37,963
  • 15
  • 156
  • 475
clines
  • 725
  • 1
  • 6
  • 10

5 Answers5

104

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.

BDM
  • 3,760
  • 3
  • 19
  • 27
  • 7
    I get `%%i was unexpected at this time.` as result when I execute above command. Not sure why. – Joel Handwell Feb 26 '16 at 16:27
  • 15
    @JoelHandwell if you are using this on the command line, you should only use one %. Ex: `for /f "delims=" %i in ('command') do set output=%i` – BDM Mar 05 '16 at 07:36
  • 5
    Using the statement in a batch file, if `command` has a pipe (|) then it errors out with: "| was unexpected at this time." Can someone help with the syntax, please? – Craig Silver Jan 04 '17 at 22:08
  • Like this the return value aka **ERRORLEVEL** gets lost. Anyone an idea how to preserve or obtain this? – Nadu Mar 11 '17 at 15:45
  • If you're trying to catch the error value, u need to redirect it to standard out in your command section. so if the command is: "dir" Then you'll need to append "2>&1"onto the end like this "dir 2>&1" – dsutherland Aug 16 '17 at 19:07
  • Not sure why, but instead of `do set` I had to do `do CALL set` – Lindsay-Needs-Sleep Apr 23 '22 at 22:49
13

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%
Community
  • 1
  • 1
Ian Kenney
  • 6,376
  • 1
  • 25
  • 44
4

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.

ElektroStudios
  • 19,105
  • 33
  • 200
  • 417
2

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. ;)
Nadu
  • 2,401
  • 1
  • 25
  • 30
1

This is work for me

@FOR /f "delims=" %i in ('reg query hklm\SOFTWARE\Macromedia\FlashPlayer\CurrentVersion') DO set var=%i
echo %var%
eQ19
  • 9,880
  • 3
  • 65
  • 77
  • 1
    it seems one percent sign (`%`) works only on command line, and in the batch file you should use double them (`%%`) – AntonK Dec 16 '22 at 12:00