首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >多%ERRORLEVEL%

多%ERRORLEVEL%
EN

Stack Overflow用户
提问于 2014-03-13 22:06:51
回答 2查看 1.6K关注 0票数 1

我有一些问题与我的当前批处理脚本,以检查版本的窗口,然后激活与某个键。这就是我到目前为止所得到的

代码语言:javascript
复制
@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
EN

回答 2

Stack Overflow用户

发布于 2014-06-28 05:16:54

解决方案1:延迟扩展

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

代码语言:javascript
复制
@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进行过滤,如上面所示,使用各种Window字符串(或版本号)来查找version of Windows。使用ver比使用

代码语言:javascript
复制
cscript /nologo c:\windows\system32\slmgr.vbs /xpr

因为ver是一个内部命令。

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

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

Microsoft

请参阅堆栈溢出问题C++: How can I return a negative value in main.cpp

任何程序员都不应该退出具有负值的应用程序。这简直是个坏主意。

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

代码语言:javascript
复制
@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
EN

Stack Overflow用户

发布于 2014-06-28 11:36:16

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

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

代码语言:javascript
复制
@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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22381137

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档