hotte Posted August 31, 2006 Share Posted August 31, 2006 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 useset 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 More sharing options...
Ctrl-X Posted August 31, 2006 Share Posted August 31, 2006 (edited) 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=%VThis 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 August 31, 2006 by Ctrl-X Link to comment Share on other sites More sharing options...
jftuga Posted August 31, 2006 Share Posted August 31, 2006 (edited) 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.txtdel c:\temp\data.txtAlso 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 August 31, 2006 by jftuga Link to comment Share on other sites More sharing options...
hotte Posted September 1, 2006 Author Share Posted September 1, 2006 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=%VThis 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 More sharing options...
hotte Posted September 1, 2006 Author Share Posted September 1, 2006 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 %Oleads 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 More sharing options...
Ctrl-X Posted September 2, 2006 Share Posted September 2, 2006 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 %Oleads 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 More sharing options...
Bilou_Gateux Posted September 9, 2006 Share Posted September 9, 2006 for /f "skip=1 tokens=*" %O in ('ver ^| sed -e "s/ \[.*\]//"') do echo %OFOR /F tokens and delimsI 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. Link to comment Share on other sites More sharing options...
Ctrl-X Posted September 9, 2006 Share Posted September 9, 2006 FOR /F tokens and delimsGreat site! And it's even made by a Dutchman Thanks for the link! Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now