DOS批处理 函数定义与用法

使用函数方便控制,bat也支持批处理啊,喜欢的朋友可以参考下

What it is, why it`s important and how to write your own.

Description: The assumption is: A batch script snippet can be named a function when:

1.... it has a callable entrance point.
2.... on completion execution continues right after the command that initially called the function.
3.... it works the same no matter from where it`s being called, even when it calls itself recursively.
4.... the variables used within a function do not conflict with variables outside the function.
5.... it exchanges data with the calling code strictly through input and output variables or a return code.

The benefits behind functions are:

1.Keep the main script clean
2.Hide complexity in reusable functions
3.Test functions independently from the main script
4.Add more functionality to your batch script simply by adding more functions at the bottom
5.Don`t worry about the function implementation, just test it and use it
 
Create a Function What is a function?
Call a Function How to invoke a function?
Example - Calling a Function An Example showing how it works.
Passing Function Arguments How to pass arguments to the function?
Parsing Function Arguments How to retrieve function arguments within the function?
Example - Function with Arguments An Example showing how it works.
Returning Values the Classic Way The classic way of returning values and the limitations.
Returning Values via References Let the caller determine how to return the function result and avoid the need of dedicated variables.
Example - Returning Values using Variable Reference An Example showing how it works.
Local Variables in Functions How to avoid name conflicts and keep variable changes local to the function?
Returning Local Variables How to pass return values over the ENDLOCAL barrier?
Recursive Functions Tadaaah!!!
Summary Defining a standard format for a DOS batch function
DOS Batch - Function Tutorial What it is, why it`s important and how to write your own.

Create a Function - What is a function
Description: In DOS you write a function by surrounding a group of command by a label and a GOTO:EOF command. A single batch file can contain multiple functions defined like this. The label becomes the function name.
Script:

复制代码 代码如下:

:myDosFunc    - here starts my function identified by it`s label
echo. here the myDosFunc function is executing a group of commands
echo. it could do a lot of things
GOTO:EOF

 

Call a Function - How to invoke a function
Description: A function can be called with the CALL command followed by the function label.
Script: 01.
 call:myDosFunc

Example - Calling a Function - An Example showing how it works
Description: The use of batch functions will divide the script into two sections.

1.The main script: starting at line 1 ending with a GOTO:EOF command that terminates the script.
2.The function section: filling the second half of the batch file with one or more functions to be callable from the main script.
 
Script:

复制代码 代码如下:

@echo off
echo.going to execute myDosFunc
call:myDosFunc
echo.returned from myDosFunc

echo.&pause&goto:eof

::--------------------------------------------------------
::-- Function section starts below here
::--------------------------------------------------------

:myDosFunc    - here starts my function identified by it`s label
echo.  here the myDosFunc function is executing a group of commands
echo.  it could do a lot of things
goto:eof

以上就是DOS批处理 函数定义与用法的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » 脚本专栏