我已经编写了一个.bat文件来首先运行一个程序,如果它正确完成,我运行另一个程序并检查它的返回值。
first-program.exe
IF "%ERRORLEVEL%"=="0" (
second-program.exe
IF "%ERRORLEVEL%"=="0" (
ECHO OK
) ELSE (
ECHO NOK
)
)
Run Code Online (Sandbox Code Playgroud)
但是,第二个%ERRORLEVEL%始终等于第一个,它不会设置为 的返回值second-program.exe。
我正在尝试将ERRORLEVEL环境变量存储到本地批处理变量中.但它总是证明是0.
CALL foo.exe
SET LEVEL=%ERRORLEVEL%
IF ERRORLEVEL 1 (
SET /A ERRORCOUNT=ERRORCOUNT+1
) ELSE (
SET /A OK=OK+1
)
ECHO/ >> logtemp.txt
ECHO ** EXIT %LEVEL% *******************************
Run Code Online (Sandbox Code Playgroud)
我试过ECHO %ERRORLEVEL%但它总是打印0出来.
foo.exe正在生成错误,可以ECHO %ERRORLEVEL%从命令提示符看到它并ERRORCOUNT正确更新.
编辑3
好的,所以看起来这可能不是安装程序问题.当我制作一个简单的批处理文件时:
exit /b 12
Run Code Online (Sandbox Code Playgroud)
并称之为
cmd /c test.cmd
echo %ERRORLEVEL%
Run Code Online (Sandbox Code Playgroud)
我在Windows Server 2003 R2上获得"12",在XP上获得"0".我以为我曾经多次测试过这个简单的测试用例,但显然没有.
所以,我已经更改了标签和标题,但是我在这里留下了其他信息,因为这里实际上有很多有用的东西与这个问题没有直接关系.
思考?
原来如下
我有一个用VBScript编写的自定义操作,而后者又调用Windows批处理文件(自定义操作基本上允许用户在安装时执行某些操作,以后也可以通过运行批处理文件来运行 - 这是一种方便).功能如下:
Function MainFunction
strCustomActionData = Session.Property("CustomActionData")
strSplit = Split(strCustomActionData, ";")
strInstallDir = strSplit(0)
strPostCopyAction = strSplit(1)
strScriptLocation = strInstallDir & "\MigrationMasterProcess.cmd"
strFullCommand = """" & strScriptLocation & """ " & strPostCopyAction
Set objShell = CreateObject("WScript.Shell")
Dim objExec
Set objExec = objShell.Exec(strFullCommand)
intReturnCode = objExec.ExitCode
Set objExec = Nothing
Set objShell = Nothing
WriteMessage "Return value: " & intReturnCode
' cf. …Run Code Online (Sandbox Code Playgroud) 批处理必须从特定位置删除文件和目录,并将成功或stdout/stderr消息输出到新的.txt文件.我创建了大部分脚本,它完全按预期执行,除非删除成功,它会向前移动到下一行,而不是在日志上回显"成功"消息.
echo Basic Deletion Batch Script > results.txt
@echo off
call :filelog >> results.txt 2>&1
notepad results.txt
exit /b
:filelog
call :delete new.txt
call :delete newer.txt
call :delete newest.txt
call :remove c:\NoSuchDirectory
GOTO :EOF
:delete
echo deleting %1
del /f /q c:\Users\newuser\Desktop\%1
if errorlevel 0 echo succesful
GOTO :EOF
:remove
echo deleting directory %1
rmdir /q /s %1
GOTO :EOF
Run Code Online (Sandbox Code Playgroud)
由于某种原因,我无法找到if del success echo'success'的语法.在上面的示例中,如果我删除该行
if errorlevel 0 echo successful
Run Code Online (Sandbox Code Playgroud)
一切正常,但没有成功的消息.留下这条线就可以说明每一条线的成功.
我希望能够在PowerShell中运行的命令提示符脚本(即)的开头将%ERRORLEVEL%环境变量(也称为“系统变量”)设置为任意值。每当人们想要设置(1)时,他们都会执行以下操作:cmd /c%ERRORLEVEL%
cmd /c "exit /b 3"
cmd /c "echo %ERRORLEVEL%"
Run Code Online (Sandbox Code Playgroud)
但是,尽管在正常命令提示符中运行上述命令时%ERRORLEVEL%设置为3,但如果在 PowerShell 中执行这些确切的行,则退出代码环境变量不会设置为3并保持为0。此外,您不能执行以下操作:
cmd /v:on /c "exit /b 3 & echo !ERRORLEVEL!"
Run Code Online (Sandbox Code Playgroud)
该exit命令完全脱离命令执行(即),并且在运行cmd /c后没有其他命令。&
因此,我尝试在PowerShell中执行以下命令:
cmd /v:on /c "SET %ERRORLEVEL% = 4 & echo !ERRORLEVEL!"
Run Code Online (Sandbox Code Playgroud)
预期输出是4,但这总是输出0。我不明白为什么我无法使用SET环境%ERRORLEVEL%变量。我已经使用了延迟命令执行(示例在这里),但这里似乎没有任何摆弄。
有谁知道为什么该命令SET %ERRORLEVEL% = …
powershell cmd exit-code errorlevel delayedvariableexpansion
%errorlevel% 在 WindowsXP 中不起作用。
据我所知,当发生错误时,%errorlevel% 设置为 1 或更高的值,如果没有错误,它将设置为 0。
但是,即使没有错误,%errorlevel% 也是 1。我设置 %errorlevel% 0,即使有错误 %errorlevel% 也是 0。
我认为操作系统不会改变 XP 中的 %errorlevel%。
在 Win7 中它完全可以工作。
@echo off
setlocal enabledelayedexpansion
call dir
echo errorlevel=%errorlevel%
REM expected errorlevel=0 but 1
call dor
echo errorlevel=%errorlevel%
REM expected errorlevel=1
set errorlevel=0
call dor
echo errorlevel=%errorlevel%
REM expected errorlevel=1 but 0
Run Code Online (Sandbox Code Playgroud)
但是如果 errorlevel 1() 看起来有效。
我一直回荡通过下面的代码我的方式,但我不能确定为什么%ERRORLEVEL%是始终为零。
@echo off
set activePerl_SiteBinPath=D:\ProgramFiles\ActivePerl\site\bin
call :isInPath %activePerl_SiteBinPath% & set foundActivePerl_SiteBinPath=%ERRORLEVEL%
echo %foundActivePerl_SiteBinPath%
set blub=d:\blub
call :isInPath %blub% & set foundBlub=%ERRORLEVEL%
echo %foundBlub%
exit /b
:isInPath
:: Tests if the path stored within variable pathVar exists within %PATH%.
::
:: The result is returned as the ERRORLEVEL:
:: 0 if pathVar is found in %PATH%.
:: 1 if pathVar path is not found in %PATH%.
:: 2 if parhVar path is missing/undefined.
:: Error checking
if …Run Code Online (Sandbox Code Playgroud) 我想得到我的教程汇编程序的退出代码(使用masm32和链接).它工作正常,我会输入echo %errorlevel%,它会显示我输入的退出代码invoke ExitProcess.现在它不再起作用了.我在OpenSuse 12.1主机和Windows Vista Home Premium上使用VirtualBox作为访客.我已经找到了答案,但已经缩短了.大多数抱怨是关于使用批处理文件,这不是我想要做的.这是一个简单的程序
hello_world.asm
.586
.model flat, stdcall
option casemap :none
include \masm32\include\windows.inc
include \masm32\include\kernel32.inc
include \masm32\include\user32.inc
includelib \masm32\lib\kernel32.lib
includelib \masm32\lib\user32.lib
.data
HelloWorld db "Hello World!", 0
.code
start:
invoke MessageBox, NULL, addr HelloWorld, addr HelloWorld, MB_OK
invoke ExitProcess, 2
end start
Run Code Online (Sandbox Code Playgroud)
我希望它返回2,但echo %errorlevel%返回0.有什么我想念的吗?谢谢,我道歉这个问题已经被解决了.我找不到答案.
编辑:实际上,我找到了部分答案.它只适用于我链接使用/SUBSYSTEM:CONSOLE.使用/SUBSYSTEM:WINDOWS始终返回0.我不知道该怎么做.带有Windows程序的退出代码在哪里?任何信息非常感谢.
errorlevel ×8
batch-file ×5
cmd ×5
exit-code ×2
windows ×2
assembly ×1
delete-file ×1
echo ×1
masm ×1
masm32 ×1
powershell ×1
subroutine ×1
windows-7 ×1