📜  批处理脚本-返回代码

📅  最后修改于: 2020-11-22 17:50:52             🧑  作者: Mango


默认情况下,当命令行执行完成时,它应该在执行成功时返回零,在执行失败时返回非零。当批处理脚本执行失败后返回非零值时,该非零值将指示错误号。然后,我们将使用错误号来确定错误的根源并相应地解决。

以下是常见的退出代码及其说明。

Error Code Description
0 Program successfully completed.
1 Incorrect function. Indicates that Action has attempted to execute non-recognized command in Windows command prompt cmd.exe.
2 The system cannot find the file specified. Indicates that the file cannot be found in specified location.
3 The system cannot find the path specified. Indicates that the specified path cannot be found.
5 Access is denied. Indicates that user has no access right to specified resource.

9009

0x2331

Program is not recognized as an internal or external command, operable program or batch file. Indicates that command, application name or path has been misspelled when configuring the Action.

221225495

0xC0000017

-1073741801

Not enough virtual memory is available.

It indicates that Windows has run out of memory.

3221225786

0xC000013A

-1073741510

The application terminated as a result of a CTRL+C. Indicates that the application has been terminated either by the user’s keyboard input CTRL+C or CTRL+Break or closing command prompt window.

3221225794

0xC0000142

-1073741502

The application failed to initialize properly. Indicates that the application has been launched on a Desktop to which the current user has no access rights. Another possible cause is that either gdi32.dll or user32.dll has failed to initialize.

错误等级

环境变量%ERRORLEVEL%包含最后执行的程序或脚本的返回码。

默认情况下,检查错误级别的方法是通过以下代码。

句法

IF %ERRORLEVEL% NEQ 0 ( 
   DO_Something 
)

通常在批处理文件的末尾使用命令EXIT / B%ERRORLEVEL%从批处理文件返回错误代码。

批处理文件末尾的EXIT / B将停止执行批处理文件。

在批处理文件的末尾使用EXIT / B 返回自定义返回码。

环境变量%ERRORLEVEL%包含批处理文件中的最新错误级别,这是最后执行的命令中的最新错误代码。在批处理文件中,始终使用环境变量而不是常量值是一个好习惯,因为同一变量在不同计算机上会扩展为不同值。

让我们看一个有关如何从批处理文件中检查错误代码的快速示例。

假设我们有一个名为Find.cmd的批处理文件,其中包含以下代码。在代码中,我们清楚地提到,如果找不到名为lists.txt的文件,则应将错误级别设置为7。类似地,如果我们看到未定义变量userprofile,则应将错误级别代码设置为9。

if not exist c:\lists.txt exit 7 
if not defined userprofile exit 9 
exit 0

假设我们有另一个名为App.cmd的文件,该文件首先调用Find.cmd。现在,如果Find.cmd返回一个错误,其中将错误级别设置为大于0,则它将退出程序。在下面的批处理文件中,调用Find.cnd查找后,实际上检查错误级别是否大于0。

Call Find.cmd

if errorlevel gtr 0 exit 
echo “Successful completion”

输出

在上面的程序中,我们可以将以下场景作为输出-

  • 如果文件c:\ lists.txt不存在,那么控制台输出中将不会显示任何内容。

  • 如果变量userprofile不存在,那么控制台输出中将不显示任何内容。

  • 如果以上两个条件均通过,则字符串“成功完成”将显示在命令提示符下。

循环

在决策一章中,我们看到了依次执行的语句。另外,也可以在批处理脚本中完成实现,以更改程序逻辑中的控制流。然后将它们分类为控制语句流。

S.No Loops & Description
1 While Statement Implementation

There is no direct while statement available in Batch Script but we can do an implementation of this loop very easily by using the if statement and labels.

2 For Statement – List Implementations

The “FOR” construct offers looping capabilities for batch files. Following is the common construct of the ‘for’ statement for working with a list of values.

3 Looping through Ranges

The ‘for’ statement also has the ability to move through a range of values. Following is the general form of the statement.

4 Classic for Loop Implementation

Following is the classic ‘for’ statement which is available in most programming languages.

遍历命令行参数

‘for’语句还可用于检查命令行参数。以下示例显示了如何使用“ for”语句循环遍历命令行参数。

@ECHO OFF 
:Loop 

IF "%1"=="" GOTO completed 
FOR %%F IN (%1) DO echo %%F 
SHIFT 
GOTO Loop 
:completed

输出

假设上面的代码存储在一个名为Test.bat的文件中。如果批处理文件将命令行参数1,2和3作为Test.bat 1 2 3传递,则上述命令将产生以下输出。

1 
2 
3
S.No Loops & Description
1 Break Statement Implementation

The break statement is used to alter the flow of control inside loops within any programming language. The break statement is normally used in looping constructs and is used to cause immediate termination of the innermost enclosing loop.