Jump to content

Redirect command output into variable


hotte

Recommended Posts

Hi there,

to check whether a particular software release is installed or not I use reg query to query the apropriate registry string:

reg query HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{98EC8E7A-161A-455B-8B31-1E88C1CDFA6B} /v DisplayVersion|find "Display"|c:\sed.exe -e "s/.*REG_SZ//g" | C:\sed.exe -e "s/^\t//"

This returns exactly the string I am looking for. Now I want to pipe the output into a variable to use it in a 'if' command but if I use

set VERSION=reg query HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{98EC8E7A-161A-455B-8B31-1E88C1CDFA6B} /v DisplayVersion|find "Display"|c:\sed.exe -e "s/.*REG_SZ//g" | C:\sed.exe -e "s/^\t//"

and query the content of %VERSION% with echo, I receive the whole command but not the result.

What am I missing here? Must the command be in some special brackets or quotes?

regards,

Sascha

Link to comment
Share on other sites


You can only set environment variables to a string value, a numerical expression (using "set /a") or a user provided input string (using "set /p"). What you are doing here is setting the variable VERSION to the command text string. In other words, the reg query command is never evaluated.

Try the following:


for /f "skip=4 tokens=3" %V in ('reg query HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{98EC8E7A-161A-455B-8B31-1E88C1CDFA6B} /v DisplayVersion') do set VERSION=%V

This will evaluate the reg query command, skip the first four lines of output and then assign the third token on the next line to the variable %V (case sensitive!), which is then assigned to the VERSION environment variable. Remember to use %%V instead of %V in a batch file. Type "for /?" in a command window for details.

Edited by Ctrl-X
Link to comment
Share on other sites

Ctrl-X,

Very nice script! I like the way you do this and will start using your idea from now on. This is what I did before:

reg query ... | sed ... > c:\temp\data.txt

set /p VERSION=<c:\temp\data.txt

del c:\temp\data.txt

Also this command:

c:\sed.exe -e "s/.*REG_SZ//g" | C:\sed.exe -e "s/^\t//"

can be shortened to just:

C:\sed.exe -e "s/.*REG_SZ//g" -e "s/^\t//"

-John

Edited by jftuga
Link to comment
Share on other sites

You can only set environment variables to a string value, a numerical expression (using "set /a") or a user provided input string (using "set /p"). What you are doing here is setting the variable VERSION to the command text string. In other words, the reg query command is never evaluated.

Try the following:


for /f "skip=4 tokens=3" %V in ('reg query HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{98EC8E7A-161A-455B-8B31-1E88C1CDFA6B} /v DisplayVersion') do set VERSION=%V

This will evaluate the reg query command, skip the first four lines of output and then assign the third token on the next line to the variable %V (case sensitive!), which is then assigned to the VERSION environment variable. Remember to use %%V instead of %V in a batch file. Type "for /?" in a command window for details.

CTRL-X,

very nice one, exactly what I was looking for! That saves even the transmission of a sed.exe to some clients!

Thanks a lot!

tot ziens!

Sascha

Link to comment
Share on other sites

Hi,

While using the command CTRL-X mentioned I came across a strange thing:

for "skip=1 tokens=*" %O in ('ver | sed -e "s/ \[.*\]//"') do echo %O

leads to an "| was unexpected at this time" message. Isn't it possible to use pipes this way?

regards,

Sascha

Link to comment
Share on other sites

While using the command CTRL-X mentioned I came across a strange thing:

for "skip=1 tokens=*" %O in ('ver | sed -e "s/ \[.*\]//"') do echo %O

leads to an "| was unexpected at this time" message. Isn't it possible to use pipes this way?

If that's the exact statement you're using, you've left out the "/f" after "for". But I've tried it myself and I'm getting the same error, so I'm afraid you're right. What string are you trying to retrieve from the output of the "ver" command? I'm not too familiar with the use of "sed", so I can't really figure it out myself.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...