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

首頁(yè) > 系統(tǒng) > DOS > 正文

DOS批處理 函數(shù)定義與用法

2020-07-26 20:14:10
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

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:

復(fù)制代碼 代碼如下:

: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:

復(fù)制代碼 代碼如下:

@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:

復(fù)制代碼 代碼如下:

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:

復(fù)制代碼 代碼如下:

 :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:

復(fù)制代碼 代碼如下:

 @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:

復(fù)制代碼 代碼如下:

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

 
Script:

復(fù)制代碼 代碼如下:

 :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:

復(fù)制代碼 代碼如下:

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

 
Script:

復(fù)制代碼 代碼如下:

 :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:

復(fù)制代碼 代碼如下:

 @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:

復(fù)制代碼 代碼如下:

 @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:

復(fù)制代碼 代碼如下:

 @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:

復(fù)制代碼 代碼如下:

 @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:

復(fù)制代碼 代碼如下:

 :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

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
伊人精品在线视频| 成人无遮挡免费网站视频在线观看| 亚洲桃色在线一区| 国产日韩欧美在线看| 国产一级淫片a视频免费观看| 日韩精品一区二区三区swag| 亚洲一区二区欧美日韩| 91在线无精精品一区二区| 亚洲一区二区三区爽爽爽爽爽| 日本精品影院| 日本一区二区在线视频观看| 精品一区免费| 免费网站看v片在线a| china中国猛gary| av一区二区三区在线观看| 中文在线免费看视频| 久久久久久久久岛国免费| 亚洲久久成人| 国产视频911| 麻豆久久久久| 久久夜色精品国产噜噜av小说| 91精品国产91久久久久久久久| 欧洲一级视频| 久久精品91久久香蕉加勒比| 日韩亚洲精品在线| 国产三区视频在线观看| 亚洲另类中文字| 婷婷亚洲久悠悠色悠在线播放| av日韩亚洲| 日本一区二区三区精品视频| 亚洲欧美国产精品专区久久| 好操啊在线观看免费视频| av色综合网| 樱花草在线观看www| 欧美黄色a视频| 国产区视频在线观看| 欧美日韩国产色| 色一情一乱一伦一区二区三区丨| 成年人免费看毛片| 国产女人高潮毛片| 色噜噜偷拍精品综合在线| 在线亚洲国产精品网站| 呦呦视频在线观看| 中日韩免视频上线全都免费| 久久综合狠狠综合久久激情| 成人免费毛片aaaaa**| 国产66精品| 国内精品久久久久国产| 国产在线日韩精品| 男女午夜激情视频| 在线播放国产视频| 国产综合激情| 国产精品国内免费一区二区三区| 日本www视频| 久久99国产精品久久99小说| 亚洲天堂av资源在线观看| 久久精品久久久久电影| 欧美日韩一区二区三区在线看| 日韩成人综合网站| 国产h视频在线播放| 成人在线资源网址| 在线观看免费版| 色片在线免费观看| 国产欧美日韩91| 国产欧美精品aaaaaa片| 在线国产福利网站| 日韩av电影免费播放| 成人免费av电影| 一区二区三区四区五区精品| 性欧美一区二区| 欧美日韩大片| 成人亚洲欧美激情在线电影| 国产极品美女高潮无套嗷嗷叫酒店| 亚洲色图综合久久| 日韩在线播放视频| 欧美性色欧美a在线播放| 秋霞久久久久久一区二区| 欧美人在线观看| 你懂的国产视频| 青娱乐国产在线| 国产一区二区免费| 777奇米四色成人影色区| 作爱视频免费观看视频在线播放激情网| 91极品女神在线| www.5588.com毛片| 亚洲a一区二区| 成人黄色在线网站| 91网页版在线| 亚洲欧美中文日韩v在线观看| 成人精品在线看| 亚洲欧美精品| 亚洲图片都市激情| 少妇无套高潮一二三区| 国产大学生视频| 一起操在线视频| 懂色av一区二区夜夜嗨| 日本一线产区和二线产区| 91精品婷婷色在线观看| caopen在线视频| 国产一区二区在线电影| 国产福利91精品一区二区三区| 国产日韩综合| 亚洲综合成人婷婷小说| 美女免费黄视频网站| 精品久久人人做人人爽| 日韩午夜激情| 亚洲人精品午夜射精日韩| 欧美一级做一级爱a做片性| 欧美多人猛交狂配| 超碰中文字幕在线| 欧美亚洲人成在线| 国产一区不卡精品| 国产精品扒开腿做爽爽爽软件| 国产成人亚洲一区二区三区| 日韩av手机在线免费观看| 国产精品久久久久久久久久尿| 轻点好疼好大好爽视频| 中文字幕免费中文| 男女爱爱视频免费| 成人午夜电影免费在线观看| 日韩久久久久久| 永久免费黄色软件| 国产在线激情| 伊人网中文字幕| 99se婷婷在线视频观看| 久久久福利影院| 91popny丨九色丨国产| 一区二区在线视频播放| 中文字幕第88页| 国产精品www色诱视频| 少妇又色又爽又黄的视频| 久久久美女艺术照精彩视频福利播放| 欧洲一级在线观看| 国产精品久久久国产盗摄| 黄网动漫久久久| 欧美日韩国产一中文字不卡| 亚洲一区在线视频观看| 国产综合中文字幕| 国产精品大全| 亚洲www啪成人一区二区麻豆| 中文字幕欧美日韩在线不卡| 丰满人妻一区二区三区四区| 在线电影国产精品| av在线不卡电影| 欧美黄色a视频| 欧洲s码亚洲m码精品一区| a级大胆欧美人体大胆666| 97在线观看视频国产| 亚洲一区免费观看| 亚洲国产精品久久久久秋霞蜜臀| 夫妇交换中文字幕| 中文字幕在线观看播放| 最爽无遮挡行房视频在线| 国产夫妻在线播放| 国产色产综合色产在线视频| 99久久夜色精品国产亚洲| 国产精品一二区| 极品美妇后花庭翘臀娇吟小说| 亚洲午夜电影网| 中文字幕欧美日韩在线| 无码免费一区二区三区免费播放| 91精品婷婷色在线观看| 久久99热狠狠色一区二区| 不卡的av一区| 亚洲精品自拍动漫在线| 同性恋视频一区| 91丝袜一区二区三区| 精品国产一区二区三区久久狼黑人| www国产视频| 日韩欧美中文在线| av电影天堂一区二区在线| 免费av网站在线看| 亚洲美女精品成人在线视频| 日韩不卡一二三区| 国产成人h网站| 啄木系列成人av电影| www.日韩av.com| 国产一区二区三区在线免费观看| 日韩成人免费在线| 日日夜夜天天综合入口| 日韩中文字幕久久| 国产精品777一区二区| 白嫩白嫩国产精品| 精品人妻一区二区三区日产乱码| 亚洲性视频h| 国产欧美在线观看视频| 中文字幕在线视频第一页| 四虎www成人影院观看| 成人黄色av网站在线| 日本全棵写真视频在线观看| 日韩精品三区| 久久久精品五月天| 黄色免费在线网站| 国产美女免费视频| 亚洲最大色网站| h网站免费在线观看| 亚洲欧美一区二区三区在线播放| 日日夜夜天天综合入口| 亚洲一区日本| 丁香六月久久综合狠狠色| 日本一区二区三区播放| 国产成人精品视频免费看| 日韩理论在线观看| 毛片av中文字幕一区二区| 久久国产精品亚洲77777| 国内精品国产成人国产三级| 永久免费看mv网站入口78| 欧美疯狂做受xxxx富婆| 秋霞毛片久久久久久久久| 青青草在线免费观看| 在线观看中文字幕亚洲| 亚洲第一成人av| 国模私拍国内精品国内av| 久久久精品在线视频| 成人av网站在线播放| 亚洲综合社区| 欧洲大片精品免费永久看nba| √天堂中文官网8在线| 欧美男男gaytwinkfreevideos| 久久久999国产| 日韩成人一级| 亚洲蜜桃在线| 麻豆91在线观看| 国产精品高颜值在线观看| 一区二区三区一级片| 91精品蜜臀在线一区尤物| 久久精品国产成人一区二区三区| 亚洲性69xxxbbb| 午夜精品久久久久99热蜜桃导演| sese在线播放| 热99这里只有精品| 依依综合在线| 精品精品国产毛片在线看| 国产在线观看无码免费视频| 91精品久久久久| 无码视频在线观看| 色丁香久综合在线久综合在线观看| 国产短剧电视剧免费观看| 国内精品视频一区二区三区| 国产成人免费av一区二区午夜| 日韩一卡二卡在线观看| 中文在线资源观看网站视频免费不卡| 濑亚美莉大战黑人中文字幕| 手机免费观看av| 91精品国产99久久久久久| 九九热精品免费视频| 夜夜嗨av一区二区三区免费区| 91麻豆精品国产无毒不卡在线观看| 久久99精品久久久久久秒播放器| 99亚洲精品| 91蝌蚪九色| 翁止熄痒禁伦短文合集免费视频| 国产精品第10页| 亚洲欧美在线磁力| 日韩欧美在线视频一区二区| 欧美成人午夜精品免费| 国产精品伦一区二区三区级视频频| 日韩一区二区三区色| 国产成人拍精品视频午夜网站| 久久久久久网站| 欧美成人乱码一二三四区免费| 国产成人精品福利| 国产女主播在线| 国产一区二区高清| 黄色免费在线观看网站| 一二三区在线视频| 欧洲一区av| 欧美—级高清免费播放| 精品国内二区三区| 日韩国产高清影视| eeuss影院www免费视频| 欧美久久精品一级黑人c片| 口述被爽到呻吟高潮自述| 欧美日本高清视频| 丝袜诱惑制服诱惑色一区在线观看| 国产亚洲va综合人人澡精品| 天堂…中文在线最新版在线| 亚洲小说区图片区| 亚洲视频在线免费观看| 人人草人人干| 亚洲黑人在线| 欧美日韩视频精品一区二区| 色又黄又爽网站www久久| 日本成人中文字幕在线| 精品一区二区三区自拍图片区| 中文字幕一区二区三区在线视频| 欧美日韩另类视频| 国产成人午夜| 欧美日韩激情视频一区二区三区| 91在线免费观看| 99久久亚洲国产日韩美女| 日韩美女毛茸茸| 都市激情久久久久久久久久久| 999在线观看免费大全电视剧| 高清性色生活片在线观看| 国产视频一二| 国产98在线| 成人高潮a毛片免费观看网站| 国产午夜亚洲精品理论片色戒| 中文字幕中文字幕一区二区| 亚洲av片在线观看| 中国china体内裑精亚洲片| 成人亲热视频网站| 国产欧美日韩久久| 中文字幕免费一区二区三区| 日韩欧美国产激情| 中文字幕无码乱码人妻日韩精品| 国产精品theporn88| 国产爆初菊在线观看免费视频网站| 日本午夜精品电影| 久热精品视频在线免费观看| 欧美性猛交xxxx乱| jvid福利写真一区二区三区| 男人的午夜天堂| 91tv亚洲精品香蕉国产一区| 动漫3d精品一区二区三区乱码| 最新欧美人z0oozo0| 国产日韩一区二区三区| 我家有个日本女人| 北条麻妃亚洲一区| 婷婷丁香一区二区三区| 欧美精品中文字幕一区二区| 欧美激情 亚洲a∨综合| 男人操女人逼免费视频| 中文在线免费看视频| 黄色av片三级三级三级免费看| 免费视频爱爱太爽了| 欧美金发大战黑人最粗videos|