This works for me:
@echo OFF
setlocal ENABLEEXTENSIONS
set KEY_NAME="HKEY_CURRENT_USERSoftwareMicrosoftCommand Processor"
set VALUE_NAME=DefaultColor
FOR /F "usebackq skip=4 tokens=1-3" %%A IN (`REG QUERY %KEY_NAME% /v %VALUE_NAME% 2^>nul`) DO (
set ValueName=%%A
set ValueType=%%B
set ValueValue=%%C
)
if defined ValueName (
@echo Value Name = %ValueName%
@echo Value Type = %ValueType%
@echo Value Value = %ValueValue%
) else (
@echo %KEY_NAME%\%VALUE_NAME% not found.
)
usebackq
is needed since the command to REG QUERY
uses double quotes.
skip=4
ignores all the output except for the line that has the value name, type and value, if it exists.
2^>nul
prevents the error text from appearing. ^
is the escape character that lets you put the >
in the for
command.
When I run the script above as given, I get this output:
Value Name = DefaultColor
Value Type = REG_DWORD
Value Value = 0x0
If I change the value of VALUE_NAME
to BogusValue
then I get this:
"HKEY_CURRENT_USERSoftwareMicrosoftCommand Processor"BogusValue not found.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…