查找注册表项并仅保存值(如果存在)



我需要查找注册表项是否存在,并且只在存在时保存值。我在变量KeyValue中没有得到任何内容。

setlocal enableextensions enabledelayexpansion
set KEY="HKLMSOFTWAREWOW6432NodeMicrosoftWindowsCurrentVersionUninstallGoogle 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

我得到的是"ECHO打开了",而不是值。我认为变量为空

我会将ERRORLEVEL处理更改为更现代的方法(src:https://ss64.com/nt/errorlevel.html)。

参见以下内容:

setlocal enableextensions enabledelayexpansion
set KEY="HKLMSOFTWAREWOW6432NodeMicrosoftWindowsCurrentVersionUninstallGoogle 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

我还修改了它,这样它就不会回声,除非没有失败。

这是一个快速注释的批处理文件示例,基于我的部分注释(不需要修改或添加(

@Rem Your registry key goes below this line.
@Set "RKey=HKLMSOFTWAREWOW6432NodeMicrosoftWindowsCurrentVersionUninstallGoogle 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开头的备注行(

最新更新