Assume I have the following registry key:
HKCU\Software\Test
Containing the string value
Sample set to "This is a test"
The best I've been able to find online is that I need to use "Reg"
like this.
Reg QUERY "hkcu\Software\Test\Sample" /v vSample
So I created the sample batch file:
@echo off
Reg QUERY "hkcu\Software\Test\Sample" /v vSample
echo Result is %vSample%
Which gave me two errors:
1) The error "Error: The system was unable to find the specified
registry key or value" was displayed and my variable was empty.
2) The error SHOULD NOT have been displayed anyway because echo was
set to off!
Even if I do the following in the command window:
Reg QUERY "hkcu\Software\Test\Sample"
I end up with:
"Error: The system was unable to find the specified registry key or
value"
Huh?
I haven't been able to do much research on this because others who
have asked this question online usually get answered in incomplete
riddles instead of a simple straightforward answer.
First, output and errors are returned to the display UNLESS they are
specifically suppressed. For example, ...
Reg QUERY "hkcu\Software\Test\" 1> nul 2>&1
The redirection to the NUL device suppresses the output. The symbols
1> and 2> are the respective redirections for StdOut and StdErr (2>&1
means send 2 to the same place as 1).
Second, you seem to have a typo in the value part of the query and REG
can only query KEYS. That is, Sample is the name of the value - which
is why the /V switch is needed. I believe it should read ...
Reg QUERY "hkcu\Software\Test" /v "Sample" 1> nul 2>&1
Third, to retrieve the contents of the value (if there is one) into a
variable you can filter the output and set an environment variable
thus ...
for /f "tokens=1,3" %%a in (
'Reg QUERY "hkcu\Software\Test" /v "Sample" 2^>nul ^| find "Sample"'
) do set %%a=%%b
This only gets the first word in the value's content, if there is more
than one. Unfortunately, the REG query does not have an easily parsed
output because it uses multiple spaces between its parts.
Maybe the Export function would work better. The one drawback to this
approach is that it requires the output to go to a file ...
Reg EXPORT "hkcu\Software\Test" tmp.txt 2> nul
for /f "tokens=2 delims==" %%a in ('find "Sample""=" ^<tmp.txt') do (
set Sample=%%~a)
del tmp.txt
echo Sample contains %Sample%
HTH,
Tom Lavedas
===========
http://members.cox.net/tglbatch/wsh/
I decided to give up entirely on reading a field from the registry.
I've decided that that level of functionality just isn't supported
with batch files.
So now I'm looking to see if certain keys exist within the registry.
Now I'd like to be able to use a simple IF statement but I can't even
get that to work or even find adequate documentation on the net for
how it's supposed to work. (I'm finding this whole exercise in
writing a batch file like trying to decipher some sort of secret code)
So this is what I've done:
Reg QUERY "HKLM\SOFTWARE\Microsoft\WINDDK\6000" 1> nul 2>&1
If not ErrorLevel 1
(
Echo ERROR: Old WinDDK detected.\r\nInstall version 6001.18001
Echo Second statement
)
And it fails with this error:
The syntax of the command is incorrect.
What syntax is wrong? From everything I've seen the proper format for
a multiple line IF statement is a set of parenthesis. Naturally
there's no real documentation to be found so I have to piece this
together by guessing from what I've seen. I could probably get around
what I need to do by using GOTOs and ELSE, but I'm sure I'll just run
into more undocumented problems.
Oh well. I think I see the light at the end of this tunnel.
----------------------------------------------------------------
@Echo off
:: Look for WinDDK 6001.18001
Reg QUERY "HKLM\SOFTWARE\Microsoft\WINDDK\x6001.18001" 1> nul 2>&1
If not ErrorLevel 1 GOTO :EOF
:: Look for old WinDDK 6000
Reg QUERY "HKLM\SOFTWARE\Microsoft\WINDDK\x6000" 1> nul 2>&1
If not ErrorLevel 1 (
::(
Echo ERROR: Old WinDDK detected. Install version 6001.18001
Echo Second statement
GOTO :EOF
)
:: Look for any WinDDK
Reg QUERY "HKLM\SOFTWARE\Microsoft\WINDDK" 1> nul 2>&1
Echo ccccccccc
If not ErrorLevel 1 (
::(
Echo dddddddddd
:: Echo ERROR: Unknown version of WinDDK detected. Version 6001.18001
is expected.
:: Echo If using a newer version update this BAT file to
support it.
:: Echo Otherwise install the newest version of the WinDDK
GOTO :EOF
}
Echo eeeeee
Echo ERROR: WinDDK not installed. This system must have WinDDK
installed.
-----------------------------------------------------------
I setup the strings defining the keys so that the third Reg command
would be hit.
Echo cccccccc is the last echo statement to output to the screen.
Echo ddddddddddd and the eeeeeeee are both ignored!
Unreal.
Not unreal at all.
if the first character on a line is a colon, the line is a LABEL, and labels
are not allowed within a compound ststement.
hence
:(
sadly, is breaking your procedure
:(
But using REM statements is valid.
Also, I notice OP has a closing bracket "}" instead of a closing parenthesis
")".
The following works for me using win95cmd.
@echo off
if x equ x (
::(
echo test1
::(
echo test2
)
But consecutive labels throws a "The system cannot find the drive specified"
message after the second label. Oddly, both echo commands do work.
::@echo off
if x equ x (
::(
::(
echo test1
::(
echo test2
)
But... If I separate them with a blank line I get, "The syntax of the
command is incorrect." and the batch stops processing.
::@echo off
if x equ x (
::(
%blank line here%
::(
echo test1
::(
echo test2
)
I agree, it's best to avoid using colons to comment out lines within
compound statements. If notation about the code is desired, place it before
the compound statement begins.
--
Todd Vargo
(Post questions to group only. Remove "z" to email personal messages)
> But using REM statements is valid.
> Also, I notice OP has a closing bracket "}" instead of a closing
> parenthesis
> ")".
>
Yes - REM statements work
But:
(parentheses)
[brackets]
{braces}
Possibly the brace-substitution is a C-hangover...