Найти ключ реестра и сохранить значение, только если оно существует - PullRequest
0 голосов
/ 31 марта 2020

Мне нужно найти, если ключ реестра существует, и сохранить значение, только если он существует. Я не получаю ничего в переменной KeyValue.

setlocal enableextensions enabledelayexpansion
set KEY="HKLM\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\Google Chrome"
for /f "tokens=3*" %%A in ('reg query %KEY% /v installocation') do set InstallLocation=%%A %%B
If ERRORLEVEL 0 set KeyValue = InstallLocation
echo %KeyValue%
cmd /k

Я получаю "ЭХО включено". вместо значения. Я думаю, что переменная пуста

Ответы [ 2 ]

2 голосов
/ 31 марта 2020

Я бы изменил обработку ERRORLEVEL на более современный подход (sr c: https://ss64.com/nt/errorlevel.html).

См. Следующее:

setlocal enableextensions enabledelayexpansion
set KEY="HKLM\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\Google Chrome"
set INSTALLLOCATION=""
set KEYVALUE=""
for /f "tokens=3*" %%A in ('reg query %KEY% /v installocation') do set %INSTALLLOCATION%=%%A %%B
If %ERRORLEVEL% EQU 0 set %KEYVALUE%=%INSTALLLOCATION%
echo %KEYVALUE% 
cmd /k

Я также изменил его, чтобы он не отображался, если не было сбоя.

0 голосов
/ 31 марта 2020

Вот краткий пример , основанный на части моего комментария, (он не требует модификации или дополнения) .

@Rem Your registry key goes below this line.
@Set "RKey=HKLM\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\Google Chrome"
@Rem Below here is where you undefine any existing value for the target variable.
@Set "VData="
@Rem Next you run your registry query, [using the correct spelling].
@For /F "EOL=HTokens=2*" %%G In ('""%__AppDir__%reg.exe" Query "%RKey%" /V "InstallLocation" 2>NUL"')Do @Set "VData=%%H"
@Rem Below is where you check to see if a value is now defined for the target. variable.
@If Not Defined VData Echo Data not found.&"%__AppDir__%timeout.exe" 3 /NoBreak>NUL&Exit /B 1
@Rem If you got here the value data existed and will be printed in the console.
@Echo %VData%
@Rem The next line was added to ensure that you get to read the printed output.
@Pause

При желании вы можете удалить все строки замечаний, (начинающиеся с @Rem) .

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...