Batch file 多个%ERRORLEVEL%

Batch file 多个%ERRORLEVEL%,batch-file,Batch File,我的当前批处理脚本在检查windows的verison,然后用某个键激活时遇到了一些问题。这就是我目前所得到的 @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 /

我的当前批处理脚本在检查windows的verison,然后用某个键激活时遇到了一些问题。这就是我目前所得到的

@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
解决方案1:延迟扩展 在示例批处理文件上应用的解决方案:

@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
@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
注意:如上所示,findstr还可以使用各种Windows字符串或版本号来筛选命令ver的输出。使用ver比使用

cscript /nologo c:\windows\system32\slmgr.vbs /xpr
因为ver是一个内部命令

解决方案2:测试错误级别 works提供的解决方案也考虑了

Microsoft提供的文档 Microsoft提供的支持文章 堆栈溢出问题 事实上,FINDSTR在退出时不返回负值。为什么? 请参阅堆栈溢出问题 任何程序员都不应该以负值退出应用程序。这简直是个坏主意。 在示例批处理文件上应用的解决方案:

@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
@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

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

&&表示错误级别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

需要使用。setlocal enabledelayedexpansion和!错误等级!感谢您的帮助:如果不是错误级别1,您也可以使用DYoudelayedexpansion@MattWilliamson这是另一种方法,但它有一个规定。如果返回的错误代码为负数,它将计算为true。同样为了澄清,if not errorlevel 1实际上表示if not errorlevel>=1。