【问题标题】:Multiple %ERRORLEVEL%多个 %ERRORLEVEL%
【发布时间】: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

【问题讨论】:

  • 需要使用delayedexpansionsetlocal enabledelayedexpansion!errorlevel!
  • 感谢您的帮助:D
  • 你也可以使用if not errorlevel 1,不需要延迟扩展
  • @MattWilliamson 这是另一种方法,但它确实有一个规定。如果返回的错误代码为负,它将评估为真。另外为了澄清,if not errorlevel 1 真的是在说if not errorlevel >= 1

标签: batch-file


【解决方案1】:

解决方案 1:延迟扩展

David Ruhmann的解决方案应用于示例批处理文件:

@echo on
setlocal enabledelayedexpansion

: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
endlocal

pause

exit /b

注意:命令ver 的输出也可以通过findstr 过滤,如上所示,使用各种Windows 字符串(或版本号)来找出version of Windows。使用ver 比使用快

cscript /nologo c:\windows\system32\slmgr.vbs /xpr

因为ver 是一个内部命令。


解决方案 2:测试错误级别

Matt Williamson 提供的解决方案也考虑到了

Matt Williamson的解决方案应用于示例批处理文件:

@echo on

:7

cscript /nologo c:\windows\system32\slmgr.vbs /xpr | findstr /i /c:"Windows(R) 7"> NUL 2>&1

if not errorlevel 1 (

    cscript /nologo c:\windows\system32\slmgr.vbs /xpr | findstr /i /c:" will expire "> NUL 2>&1

    if not errorlevel 1 (

    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 not errorlevel 1 (

    cscript /nologo c:\windows\system32\slmgr.vbs /xpr | findstr /i /c:" will expire "> NUL 2>&1

    if not errorlevel 1 (

    slmgr.vbs /ipk XXXXX-XXXXX-XXXXX-XXXXX-XXXXX
    slmgr.vbs /ato

    ) else (GOTO END)

) else (GOTO END)

:END

pause

exit /b

【讨论】:

    【解决方案2】:

    这是安排脚本的另一种方法:

    && 表示错误级别 0,反之 || 表示错误级别 1

    @echo off
    
    cscript /nologo c:\windows\system32\slmgr.vbs /xpr | findstr /i /c:"Windows(R) 7"> NUL 2>&1 && set num=1XXXX-XXXXX-XXXXX-XXXXX-XXXXX
    cscript /nologo c:\windows\system32\slmgr.vbs /xpr | findstr /i /c:"Windows(R) Vista"> NUL 2>&1 && set num=2XXXX-XXXXX-XXXXX-XXXXX-XXXXX
    
    cscript /nologo c:\windows\system32\slmgr.vbs /xpr | findstr /i /c:" will expire "> NUL 2>&1 && (
    
        slmgr.vbs /ipk %num%
        slmgr.vbs /ato
    
    )
    
    pause
    
    exit /b
    

    【讨论】:

      猜你喜欢
      • 2015-01-23
      • 2011-05-21
      • 1970-01-01
      • 2013-05-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-13
      • 2012-12-13
      相关资源
      最近更新 更多