📅  最后修改于: 2020-11-22 17:51:19             🧑  作者: Mango
函数是一起组织以执行特定任务的一组语句。在批处理脚本中,采用了类似的方法将逻辑语句组合在一起以形成一个函数。
像其他任何语言一样,批处理脚本中的函数遵循相同的过程-
函数声明-它告诉编译器函数的名称,返回类型和参数。
功能定义-它提供了函数的实际身体。
在批处理脚本中,通过使用label语句定义函数。重新定义一个函数,它可以将一个或多个值作为函数的输入“参数”,在主体中处理函数,然后将这些值作为输出“返回类型”传递回函数。
每个函数都有一个函数名称,该名称描述了该函数执行的任务。要使用一个函数,您要“调用”该函数的名称并传递与该函数的参数类型匹配的输入值(称为自变量)。
以下是一个简单函数的语法。
:function_name
Do_something
EXIT /B 0
该函数名是给它应该有一定的意义,内容相匹配的函数实际执行函数的名称。
EXIT语句用于确保函数正确退出。
以下是一个简单函数的示例。
:Display
SET /A index=2
echo The value of index is %index%
EXIT /B 0
S.No | Functions & Description |
---|---|
1 | Calling a Function
A function is called in Batch Script by using the call command. |
2 | Functions with Parameters
Functions can work with parameters by simply passing them when a call is made to the function. |
3 | Functions with Return Values
Functions can work with return values by simply passing variables names |
4 | Local Variables in Functions
Local variables in functions can be used to avoid name conflicts and keep variable changes local to the function. |
5 | Recursive Functions
The ability to completely encapsulate the body of a function by keeping variable changes local to the function and invisible to the caller. |
6 | File I/O
In Batch Script, it is possible to perform the normal file I/O operations that would be expected in any programming language. |
7 | Creating Files
The creation of a new file is done with the help of the redirection filter >. This filter can be used to redirect any output to a file. |
8 | Writing to Files
Content writing to files is also done with the help of the redirection filter >. This filter can be used to redirect any output to a file. |
9 | Appending to Files
Content writing to files is also done with the help of the double redirection filter >>. This filter can be used to append any output to a file. |
10 | Reading from Files
Reading of files in a batch script is done via using the FOR loop command to go through each line which is defined in the file that needs to be read. |
11 | Deleting Files
For deleting files, Batch Script provides the DEL command. |
12 | Renaming Files
For renaming files, Batch Script provides the REN or RENAME command. |
13 | Moving Files
For moving files, Batch Script provides the MOVE command. |
14 | Batch Files – Pipes
The pipe operator (|) takes the output (by default, STDOUT) of one command and directs it into the input (by default, STDIN) of another command. |
15 | Batch Files – Inputs
When a batch file is run, it gives you the option to pass in command line parameters which can then be read within the program for further processing. |
16 | Using the SHIFT Operator
One of the limitations of command line arguments is that it can accept only arguments till %9. Let’s take an example of this limitation. |
17 | Folders
In Batch Script, it is possible to perform the normal folder based operations that would be expected in any programming language. |
18 | Creating Folders
The creation of a folder is done with the assistance of the MD (Make directory) command. |
19 | Listing Folder Contents
The listing of folder contents can be done with the dir command. This command allows you to see the available files and directories in the current directory. |
20 | Deleting Folders
For deleting folders, Batch Scripting provides the DEL command. |
21 | Renaming Folders
For renaming folders, Batch Script provides the REN or RENAME command. |
22 | Moving Folders
For moving folders, Batch Script provides the MOVE command. |