44

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
  • 27,258
  • 11
  • 104
  • 360
user2283234
  • 577
  • 1
  • 4
  • 8
  • If the below answers didn't help, search redirection on google. – BDM Apr 25 '13 at 21:25
  • possible duplicate of [Batch equivalent of Bash backticks](http://stackoverflow.com/questions/2768608/batch-equivalent-of-bash-backticks) – Cristian Ciupitu Jul 01 '14 at 16:51
  • Actually the original seems to be [How do I get the result of a command in a variable in windows?](http://stackoverflow.com/q/108439/12892). – Cristian Ciupitu Jul 01 '14 at 16:54
  • possible duplicate of [Batch Files - How to set commands output as a variable](http://stackoverflow.com/questions/6359820/batch-files-how-to-set-commands-output-as-a-variable) – Nathan Aug 13 '14 at 23:38

5 Answers5

93

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,324
  • 2
  • 17
  • 27
  • 4
    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
  • 8
    @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
  • 4
    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
12

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
  • 5,661
  • 1
  • 21
  • 39
3

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
  • 17,150
  • 31
  • 162
  • 376
1

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,058
  • 1
  • 20
  • 27
0

This is work for me

@FOR /f "delims=" %i in ('reg query hklm\SOFTWARE\Macromedia\FlashPlayer\CurrentVersion') DO set var=%i
echo %var%
Chetabahana
  • 7,806
  • 2
  • 50
  • 70