我正在尝试创建一个批处理脚本(仅是特定的批处理脚本),以检查我的计算机是否安装了最新的 Microsoft Office 版本。如果它检测到最新版本,它将输出 hello,否则它将输出 heyo。但是,if else 语句似乎不起作用,因为即使安装了正确版本的 Office,我仍然不断收到回显“heyo”。
我尝试过的:
@echo off
for /f "skip=1 tokens=3 delims= " %%a in ('
reg query "HKEY_LOCALMACHINE\SOFTWARE\Microsoft\Office\ClickToRun\Configuration" /v VersionToReport
') do set "VersionToReport=%%~a"
IF "%VersionToReport%" EQU "16.0.10396.20023" (
echo hello
) else (
echo heyo
)
答案1
除非您确实需要将当前版本号存储在变量中,否则最简单的方法是执行命令( )并在输出中reg query
搜索您想要的字符串( )。16.0.10396.20023
然后您可以使用的值%errorlevel%
来确定是否找到它,如下所示:
reg query "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Office\ClickToRun\Configuration" /v VersionToReport | findstr "16.0.10396.20023"
if %errorlevel% equ 0 (
echo Version is okay
) else (
echo Version is not okay
)
答案2
@echo off
2>nul reg query "HKLM\SOFTWARE\Microsoft\Office\ClickToRun\Configuration" /v VersionToReport | find "16.0.10396.20023" >nul && echo\hello || echo\heyo
- 或者...
@echo off
set "_Hkey="HKLM\SOFTWARE\Microsoft\Office\ClickToRun\Configuration" /v VersionToReport
2>nul reg.exe query %_Hkey% | find "16.0.10396.20023" >nul && echo\hello || echo\heyo
您可以使用运算符:
command && ( run if return 0)
command || ( run if return non 0)
command && echo\is zOk || echo\isn't ok
@echo off
for /f "skip=1 tokens=3 delims= " %%a in (
'2^>nul reg query "HKLM\SOFTWARE\Microsoft\Office\ClickToRun\Configuration" /v VersionToReport'
)do set "VersionToReport=%%~a" && if "%%~a" == "16.0.10396.20023" (echo\hello)else echo\heyo
if defined VersionToReport (
echo; Version: %VersionToReport%)else echo\Wrong version!