亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb

首頁 > 系統 > DOS > 正文

DOS批處理 函數定義與用法

2020-07-26 20:14:10
字體:
來源:轉載
供稿:網友

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


 
Script Output:   Script Output 
going to execute myDosFunc
  here the myDosFunc function is executing a group of commands
  it could do a lot of things
returned from myDosFunc
Press any key to continue . . .
 
Passing Function Arguments - How to pass arguments to the function
Description: Just as the DOS batch file itself can have arguments, a function can be called with arguments in a similar way. Just list all arguments after the function name in the call command.
Use a space or a comma to separate arguments.
Use double quotes for string arguments with spaces.
Script:

復制代碼 代碼如下:

call:myDosFunc 100 YeePEE
call:myDosFunc 100 "for me"
call:myDosFunc 100,"for me"

 
Parsing Function Arguments - How to retrieve function arguments within the function
Description: Just as the DOS batch file itself retrieves arguments via %1 … %9 a function can parse it`s arguments the same way. The same rules apply.
Let`s modify our previews example to use arguments.
To strip of the double quotes in an arguments value the tilde modifier, i.e. use %~2 instead of %2.
Script:

復制代碼 代碼如下:

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

 
Example - Function with Arguments - An Example showing how it works
Description: The following example demonstrates how to pass arguments into a DOS function. The :myDosFunc function is being called multiple times with different arguments.

Note: The last call to myDosFunc doesn`t use double quotes for the second argument. Subsequently "for" and "me" will be handled as two separate arguments, whereas the third argument "me" is not being used within the function.
Script:

復制代碼 代碼如下:

 @echo off
echo.going to execute myDosFunc with different arguments
call:myDosFunc 100 YeePEE
call:myDosFunc 100 "for me"
call:myDosFunc 100,"for me"
call:myDosFunc 100,for me
echo.&pause&goto:eof

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

:myDosFunc    - here starts my function identified by it's label
echo.
echo. here the myDosFunc function is executing a group of commands
echo. it could do %~1 of things %~2.
goto:eof


 
Script Output:   Script Output 
going to execute myDosFunc with different arguments
 
 here the myDosFunc function is executing a group of commands
 it could do 100 of things YeePEE.
 
 here the myDosFunc function is executing a group of commands
 it could do 100 of things for me.
 
 here the myDosFunc function is executing a group of commands
 it could do 100 of things for me.
 
 here the myDosFunc function is executing a group of commands
 it could do 100 of things for.
 
Press any key to continue . . .
 

Returning Values the Classic Way - The classic way of returning values and the limitations
Description: The CALL command doesn`t support return values as known by other programming languages.
The classic walkaround is to have the function store the return value into a environment variable. The calling script can use this variable when the function returns. The :myGetFunc function below demonstrates how the variable var1 gets the "DosTips" string assigned which can then be used in the calling function.

Note: The var1 variable is reserved for this particular function. Any data stored in var1 by the calling function before calling :myGetVar will be overwritten.
Usage:

復制代碼 代碼如下:

 set "var1=some hopefully not important string"
echo.var1 before: %var1%
call:myGetFunc
echo.var1 after : %var1%

 
Script:

復制代碼 代碼如下:

 :myGetFunc    - get a value
set "var1=DosTips"
goto:eof

 
Script Output:   Script Output 
var1 before: some hopefully not important string
var1 after : DosTips
 
Returning Values via References - Let the caller determine how to return the function result and avoid the need of dedicated variables
Description: Instead of using "global" variables for return value, the function can use one of it`s arguments as variable reference. The caller can then pass a variable name to the function and the function can store the result into this variable making use of the command line expansion of the command processor:

Note: The var1 variable is not reserved for this articular function. Any variable can be passed to the function the caller has full control.
Usage:

復制代碼 代碼如下:

 call:myGetFunc var1
echo.var1 after : %var1%

 
Script:

復制代碼 代碼如下:

 :myGetFunc    - passing a variable by reference
set "%~1=DosTips"
goto:eof

 
Script Output:   Script Output 
var1 after : DosTips
 
Example - Returning Values using Variable Reference - An Example showing how it works
Description: This code shows how the var1 variable is being passed into a :myGetFunc function simply by passing the variable name. Within the :myGetFunc function the command processor works like this:
1.Reads the set command into memory: set "%~1=DosTips"
2.Expand the variables, i.e. %~1 like this: set "var1=DosTips"
3.Finally execute the command and assign the new string to var1
 
Script:

復制代碼 代碼如下:

 @echo off

set "var1=CmdTips"
echo.var1 before: %var1%
call:myGetFunc var1
echo.var1 after : %var1%

echo.&pause&goto:eof


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

:myGetFunc    - passing a variable by reference
set "%~1=DosTips"
goto:eof


 
Script Output:   Script Output 
var1 before: CmdTips
var1 after : DosTips
 
Press any key to continue . . .
 

Local Variables in Functions - How to avoid name conflicts and keep variable changes local to the function
Description: The SETLOCAL causes the command processor to backup all environment variables. The variables can be restored by calling ENDLOCAL. Changes made im between are local to the current batch. ENDLOCAL is automatically being called when the end of the batch file is reached, i.e. by calling GOTO:EOF.
Localizing variables with SETLOCAL allows using variable names within a function freely without worrying about name conflicts with variables used outside the function.
Script:

復制代碼 代碼如下:

 @echo off

set "aStr=Expect no changed, even if used in function"
set "var1=No change for this one.  Now what?"
echo.aStr before: %aStr%
echo.var1 before: %var1%
call:myGetFunc var1
echo.aStr after : %aStr%
echo.var1 after : %var1%

echo.&pause&goto:eof

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

:myGetFunc    - passing a variable by reference
SETLOCAL
set "aStr=DosTips"
set "%~1=%aStr%"
ENDLOCAL
goto:eof


 
Script Output:   Script Output 
aStr before: Expect no changed, even if used in function
var1 before: No change for this one.  Now what?
aStr after : Expect no changed, even if used in function
var1 after : No change for this one.  Now what?
 
Press any key to continue . . .
 
Returning Local Variables - How to pass return values over the ENDLOCAL barrier
Description: The question is: When localizing a function via SETLOCAL and ENDLOCAL, how to return a value that was calculated before executing ENDLOCAL when ENDLOCAL restores all variables back to its original state?
The answer comes with "variable expansion". The command processor expands all variables of a command before executing the command. Letting the command processor executing ENDLOCAL and a SET command at once solves the problem. Commands can be grouped within brackets.
Script:

復制代碼 代碼如下:

 @echo off

set "aStr=Expect no changed, even if used in function"
set "var1=Expect changed"
echo.aStr before: %aStr%
echo.var1 before: %var1%
call:myGetFunc var1
echo.aStr after : %aStr%
echo.var1 after : %var1%

echo.&pause&goto:eof

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

:myGetFunc    - passing a variable by reference
SETLOCAL
set "aStr=DosTips"
( ENDLOCAL
    set "%~1=%aStr%"
)
goto:eof

:myGetFunc2    - passing a variable by reference
SETLOCAL
set "aStr=DosTips"
ENDLOCAL&set "%~1=%aStr%"       &rem THIS ALSO WORKS FINE
goto:eof


 
Script Output:   Script Output 
aStr before: Expect no changed, even if used in function
var1 before: Expect changed
aStr after : Expect no changed, even if used in function
var1 after : DosTips
 
Press any key to continue . . .

Recursive Functions - Tadaaah!!!
Description: Being able to completely encapsulate the body of a function by keeping variable changes local to the function and invisible to the caller we are now able to call a function recursively making sure each level of recursion works with its own set of variables even thought variable names are being reused.

Example: The next example below shows how to calculate a Fibonacci number recursively. The recursion ss when the Fibonacci algorism reaches a number greater or equal to a given input number.
The example starts with the numbers 0 and 1 the :myFibo function calls itself recursively to calculate the next Fibonacci number until it finds the Fibonacci number greater or equal 1000000000.

The first argument of the myFibo function is the name of the variable to store the output in. This variable must be initialized to the Fibonacci number to start with and will be used as current Fibonacci number when calling the function and will be set to the subsequent Fibonacci number when the function returns.
Script:

復制代碼 代碼如下:

 @echo off

set "fst=0"
set "fib=1"
set "limit=1000000000"
call:myFibo fib,%fst%,%limit%
echo.The next Fibonacci number greater or equal %limit% is %fib%.

echo.&pause&goto:eof


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

:myFibo  -- calculate recursively the next Fibonacci number greater or equal to a limit
::       -- %~1: return variable reference and current Fibonacci number
::       -- %~2: previous value
::       -- %~3: limit
SETLOCAL
set /a "Number1=%~1"
set /a "Number2=%~2"
set /a "Limit=%~3"
set /a "NumberN=Number1 + Number2"
if /i %NumberN% LSS %Limit% call:myFibo NumberN,%Number1%,%Limit%
(ENDLOCAL
    IF "%~1" NEQ "" SET "%~1=%NumberN%"
)
goto:eof


 
Script Output:   Script Output 
The next Fibonacci number greater or equal 1000000000 is 1134903170.
 
Press any key to continue . . .
 
Summary - Defining a standard format for a DOS batch function
Description: With the information learned in this section we can define a standard format for a DOS batch functions as shown below.
Also check out the rich set of ready to use DOS functions provided by the DosTips.com function library.
Script:

復制代碼 代碼如下:

 :myFunctionName    -- function description here
::                 -- %~1: argument description here
SETLOCAL
REM.--function body here
set LocalVar1=...
set LocalVar2=...
(ENDLOCAL & REM -- RETURN VALUES
    IF "%~1" NEQ "" SET %~1=%LocalVar1%
    IF "%~2" NEQ "" SET %~2=%LocalVar2%
)
GOTO:EOF

出處:http://www.dostips.com/DtTutoFunctions.php

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb
久久久视频免费观看| www.欧美免费| 中日韩午夜理伦电影免费| 欧美一级高清免费| 欧美一级片在线播放| 色婷婷综合久久久久中文字幕1| 2020欧美日韩在线视频| 最近2019中文字幕一页二页| 久久精品电影一区二区| 国产一区二区在线免费| 久久躁日日躁aaaaxxxx| 一区二区在线视频播放| 97久久超碰福利国产精品…| 国产aⅴ夜夜欢一区二区三区| 国产97在线视频| 日韩欧美有码在线| 亚洲国产免费av| 欧美性受xxx| 日韩中文字幕第一页| 久久久91精品| 亚洲精品一区二区网址| 久久九九全国免费精品观看| 亚洲免费电影一区| 成人亚洲欧美一区二区三区| 久热国产精品视频| 亚洲女同性videos| 亚洲第一中文字幕在线观看| 欧美日韩午夜视频在线观看| 国产91ⅴ在线精品免费观看| 国产精品青青在线观看爽香蕉| 在线观看中文字幕亚洲| 精品国产乱码久久久久酒店| 亚洲国产精品字幕| 日韩在线免费观看视频| 91中文字幕在线观看| 国产欧美韩国高清| 66m—66摸成人免费视频| 韩国v欧美v日本v亚洲| 日韩高清电影好看的电视剧电影| 欧美精品在线免费观看| 日韩欧美在线网址| 久久久免费在线观看| 国产精品h片在线播放| 一个人www欧美| 一级做a爰片久久毛片美女图片| 亚洲成人999| 国产精品老女人精品视频| 亚洲自拍另类欧美丝袜| 亚洲毛茸茸少妇高潮呻吟| 78m国产成人精品视频| 国产一区二区三区久久精品| 色久欧美在线视频观看| 亚洲激情在线观看视频免费| 日韩美女av在线| 国产成人久久久| 欧美视频免费在线观看| 国产精品久久久久久久久久久新郎| 色老头一区二区三区在线观看| 成人精品视频在线| 日韩在线视频国产| 97热精品视频官网| 欧美日本精品在线| 怡红院精品视频| 色樱桃影院亚洲精品影院| 中文字幕视频在线免费欧美日韩综合在线看| 狠狠躁天天躁日日躁欧美| 久久精品成人动漫| 国产欧美日韩丝袜精品一区| 亚洲欧洲日产国码av系列天堂| 亚洲女人被黑人巨大进入| 国产精品18久久久久久首页狼| 国产成人在线播放| 久久久人成影片一区二区三区观看| 亚洲天堂av在线免费观看| 亚洲欧美第一页| 欧美激情第6页| 久久夜色精品国产| 日韩av电影手机在线观看| 综合久久五月天| 日韩欧美亚洲一二三区| 亚洲级视频在线观看免费1级| 亚洲欧美精品suv| 日韩网站免费观看高清| 久久综合伊人77777| 国产一区二区三区在线视频| 欧美成人免费在线观看| 一区二区欧美激情| 欧美丰满片xxx777| 国产小视频91| 久久久久久国产精品久久| 亚洲欧美另类自拍| 欧美激情精品久久久久久久变态| 久久综合88中文色鬼| 欧美老女人性视频| 国产精品久久77777| 国产欧美精品一区二区三区-老狼| 久久久久久高潮国产精品视| 日本精品久久中文字幕佐佐木| 亚洲国产成人在线视频| 欧美大成色www永久网站婷| 亚洲成人网久久久| 日韩电视剧免费观看网站| 亚洲精品一区久久久久久| 亚洲毛片在线观看| 日韩av中文字幕在线| 777午夜精品福利在线观看| 国产一区二区三区精品久久久| 98精品国产自产在线观看| 精品国产美女在线| 欧美大片免费观看在线观看网站推荐| 91成品人片a无限观看| 亚洲国产精品一区二区久| 亚洲成年网站在线观看| 91精品国产综合久久香蕉922| 欧美视频在线视频| 性欧美长视频免费观看不卡| 亚洲a在线播放| 91经典在线视频| 亚洲第一中文字幕在线观看| 午夜免费在线观看精品视频| 午夜精品视频在线| 欧美一区二区三区免费观看| 97精品国产97久久久久久免费| 精品久久久久久久久久| 亚洲一区亚洲二区| 亚洲成人久久网| 国产成人一区二区三区小说| 日韩国产精品亚洲а∨天堂免| 色阁综合伊人av| 欧美日在线观看| 成人亚洲综合色就1024| 亚洲精品久久久久久久久久久| 成人激情视频小说免费下载| 色妞色视频一区二区三区四区| 91国产精品电影| 欧美日韩国产第一页| 韩日精品中文字幕| 夜夜躁日日躁狠狠久久88av| 91高清免费视频| 国产精品一区二区三区久久久| 国内精品久久久久久中文字幕| 欧洲午夜精品久久久| 全色精品综合影院| 国产精品www| 国产精品入口日韩视频大尺度| 米奇精品一区二区三区在线观看| 久久久久久久久久婷婷| 国产精品一区电影| 国产精品高潮呻吟视频| 国产日韩欧美在线观看| 国产精品视频26uuu| 日韩精品高清在线| 亚洲精品久久久久久久久久久久久| 日韩成人免费视频| 国产精品91在线| 永久免费毛片在线播放不卡| 亚洲欧美国产精品va在线观看| 国产激情久久久久| 不卡av电影在线观看| 国产精品入口夜色视频大尺度| 不用播放器成人网| 成人有码视频在线播放| 欧美日韩精品中文字幕| 国语自产在线不卡|