Jump to content

Welcome to MSFN Forum
Register now to gain access to all of our features. Once registered and logged in, you will be able to create topics, post replies to existing threads, give reputation to your fellow members, get your own private messenger, post status updates, manage your profile and so much more. This message will be removed once you have signed in.
Login to Account Create an Account


Photo

Redirect command output into variable

- - - - -

  • Please log in to reply
7 replies to this topic

#1
hotte

hotte
  • Member
  • 6 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


How to remove advertisement from MSFN

#2
Ctrl-X

Ctrl-X

    Advanced Member

  • Member
  • PipPipPip
  • 444 posts
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, 31 August 2006 - 08:47 AM.


#3
jftuga

jftuga

    Member

  • Member
  • PipPip
  • 283 posts
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, 31 August 2006 - 01:54 PM.


#4
hotte

hotte
  • Member
  • 6 posts

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

#5
hotte

hotte
  • Member
  • 6 posts
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

#6
Ctrl-X

Ctrl-X

    Advanced Member

  • Member
  • PipPipPip
  • 444 posts

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.

#7
Bilou_Gateux

Bilou_Gateux

    Powered by Windows Embedded

  • Member
  • PipPipPipPipPip
  • 768 posts
for /f "skip=1 tokens=*" %O in ('ver ^| sed -e "s/ \[.*\]//"') do echo %O

FOR /F tokens and delims

I will skip the details right now, just remember to place a caret before pipe and redirection characters when used within brackets of FOR /F commands.


OS Version = 5.1.2600 Service Pack 3
Platform ID = 2 (NT)
Service Pack = 3.0
Suite = 0x0140
Product Type = 1
Architecture = x86

#8
Ctrl-X

Ctrl-X

    Advanced Member

  • Member
  • PipPipPip
  • 444 posts

FOR /F tokens and delims

Great site! And it's even made by a Dutchman :rolleyes: Thanks for the link!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users



How to remove advertisement from MSFN