【发布时间】:2014-03-13 14:06:51
【问题描述】:
我当前的批处理脚本存在一些问题,无法检查 Windows 的版本,然后使用某个键激活。这就是我目前所得到的
@echo on
:7
cscript /nologo c:\windows\system32\slmgr.vbs /xpr | findstr /i /c:"Windows(R) 7"> NUL 2>&1
if [%errorlevel%]==[0] (
cscript /nologo c:\windows\system32\slmgr.vbs /xpr | findstr /i /c:" will expire "> NUL 2>&1
if [%errorlevel%]==[0] (
slmgr.vbs /ipk XXXXX-XXXXX-XXXXX-XXXXX-XXXXX
slmgr.vbs /ato
) else (GOTO END)
) else (GOTO VISTA)
:VISTA
cscript /nologo c:\windows\system32\slmgr.vbs /xpr | findstr /i /c:"Windows(R) Vista"> NUL 2>&1
if [%errorlevel%]==[0] (
cscript /nologo c:\windows\system32\slmgr.vbs /xpr | findstr /i /c:" will expire "> NUL 2>&1
if [%errorlevel%]==[0] (
slmgr.vbs /ipk XXXXX-XXXXX-XXXXX-XXXXX-XXXXX
slmgr.vbs /ato
) else (GOTO END)
) else (GOTO END)
:END
pause
exit /b
【问题讨论】:
-
需要使用delayedexpansion。
setlocal enabledelayedexpansion和!errorlevel! -
感谢您的帮助:D
-
你也可以使用
if not errorlevel 1,不需要延迟扩展 -
@MattWilliamson 这是另一种方法,但它确实有一个规定。如果返回的错误代码为负,它将评估为真。另外为了澄清,
if not errorlevel 1真的是在说if not errorlevel >= 1。
标签: batch-file