婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av

主頁 > 知識庫 > DOS批處理 函數定義與用法

DOS批處理 函數定義與用法

熱門標簽:呼倫貝爾外呼系統 如何申請400電話業務mm 玉林市機器人外呼系統哪家好 雷霆電銷機器人怎么樣 智能打電話機器人收費 電話機器人產品怎么樣 電話機器人全國招商 清遠百度地圖標注店鋪位置 如何弄地圖標注

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.pausegoto: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.pausegoto: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.pausegoto: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.pausegoto: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.pausegoto: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"
ENDLOCALset "%~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.pausegoto: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

標簽:樂山 白銀 公主嶺 株洲 蕪湖 江西 臺州 三門峽

巨人網絡通訊聲明:本文標題《DOS批處理 函數定義與用法》,本文關鍵詞  DOS,批處理,函數,定義,與,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《DOS批處理 函數定義與用法》相關的同類信息!
  • 本頁收集關于DOS批處理 函數定義與用法的相關信息資訊供網民參考!
  • 推薦文章
    婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av
    亚洲国产婷婷综合在线精品| 日本最新不卡在线| 午夜日韩在线电影| 精品一区二区三区免费视频| 国产一区二区导航在线播放| 欧美亚洲一区二区在线| 欧美精品自拍偷拍| 国产精品99久久久久久久vr| 国产风韵犹存在线视精品| 在线播放中文字幕一区| 国产老肥熟一区二区三区| 亚洲一区视频在线观看视频| 亚洲一卡二卡三卡四卡无卡久久| 国产精品第五页| 亚洲v中文字幕| 国产一二三精品| 综合久久久久久久| 欧美精品v日韩精品v韩国精品v| 日本韩国精品在线| 欧美日韩卡一卡二| 亚洲人成网站色在线观看| 欧美综合视频在线观看| 日韩欧美资源站| 久久99最新地址| 中日韩免费视频中文字幕| 蜜臀a∨国产成人精品| 午夜私人影院久久久久| 成人激情动漫在线观看| 久久综合久久鬼色中文字| 亚洲综合精品自拍| 色婷婷精品大在线视频 | 亚洲欧美视频一区| 精品精品欲导航| 欧美日韩电影一区| 国产一区二区三区高清播放| 国产精品久久久久影院老司 | 日韩精品高清不卡| 中文字幕电影一区| 日韩一级完整毛片| 91国偷自产一区二区三区观看 | 激情伊人五月天久久综合| 国产精品久久综合| 日韩一区二区三区电影| 色综合视频在线观看| 狠狠狠色丁香婷婷综合久久五月| 又紧又大又爽精品一区二区| 精品国精品国产| 欧美日韩激情在线| 91麻豆免费观看| 麻豆一区二区三| 亚洲国产精品久久艾草纯爱| 亚洲天堂免费在线观看视频| 久久看人人爽人人| 欧美成人一区二区| 日韩欧美国产不卡| 69av一区二区三区| 欧美在线播放高清精品| 在线视频一区二区三| jlzzjlzz亚洲女人18| 国产91精品精华液一区二区三区 | 中文字幕亚洲电影| 国产精品丝袜黑色高跟| 国产亚洲短视频| 久久久亚洲高清| 精品奇米国产一区二区三区| 91精品在线麻豆| 欧美va在线播放| 久久精品视频一区二区| 中国色在线观看另类| 国产精品―色哟哟| 亚洲欧洲国产日本综合| 一区二区三区在线免费观看| 亚洲香蕉伊在人在线观| 樱花草国产18久久久久| 亚洲国产欧美在线| 日韩国产高清影视| 国模冰冰炮一区二区| 豆国产96在线|亚洲| 99久久夜色精品国产网站| 色婷婷av久久久久久久| 欧美网站一区二区| 欧美成人在线直播| 中文字幕一区二区三区在线播放| 一区二区三区加勒比av| 麻豆国产一区二区| 成人激情黄色小说| 6080日韩午夜伦伦午夜伦| 久久久久久99久久久精品网站| 国产精品久久久久久一区二区三区| 亚洲日本一区二区三区| 天天综合天天综合色| 激情成人综合网| 一本久久精品一区二区| 日韩视频一区在线观看| 国产精品国产三级国产aⅴ中文| 亚洲自拍偷拍欧美| 精品一区二区三区影院在线午夜| 床上的激情91.| 在线电影一区二区三区| 中文字幕一区二区视频| 久久97超碰国产精品超碰| 99久久免费精品| 精品国产成人在线影院| 一区二区三区.www| 国产乱码精品一区二区三区av | 精品国产人成亚洲区| 国产精品不卡视频| 激情文学综合网| 欧美日韩夫妻久久| 欧美高清在线精品一区| 亚洲国产sm捆绑调教视频| 成人网在线免费视频| 欧美一区二区久久久| 亚洲女人****多毛耸耸8| 国产精品18久久久久久vr| 欧美日韩一区三区| 国产精品看片你懂得| 免费观看久久久4p| 欧美精品在线观看播放| 亚洲男女毛片无遮挡| 国产98色在线|日韩| 欧美电影免费观看高清完整版在线| 亚洲精品国产一区二区精华液| 国产福利一区二区| 精品电影一区二区三区| 免费成人美女在线观看| 欧美剧在线免费观看网站 | 在线观看视频欧美| 国产欧美日韩中文久久| 日本午夜精品视频在线观看| 91久久免费观看| 亚洲丝袜美腿综合| www.激情成人| 日韩免费一区二区| 亚洲精品免费在线| 色综合久久精品| 亚洲图片另类小说| 国产福利91精品| 国产精品欧美经典| 99re这里只有精品首页| 亚洲色图丝袜美腿| 91在线丨porny丨国产| 自拍偷拍欧美精品| 972aa.com艺术欧美| 亚洲精品少妇30p| 在线一区二区视频| 国产亚洲午夜高清国产拍精品 | 一本久久a久久精品亚洲| 狠狠色丁香久久婷婷综| 亚洲一卡二卡三卡四卡五卡| 奇米影视在线99精品| 国产激情偷乱视频一区二区三区| 欧美丝袜丝交足nylons| 国产精品欧美一区喷水| 丁香五精品蜜臀久久久久99网站| 懂色av一区二区在线播放| 国产清纯白嫩初高生在线观看91 | 91久久国产最好的精华液| 国产精品嫩草影院com| 成人综合日日夜夜| 国产精品国产a| 欧美日韩免费一区二区三区| 日本不卡不码高清免费观看| 国产亚洲欧美中文| 中文字幕亚洲在| 激情小说欧美图片| 欧美日韩国产bt| 久久久不卡网国产精品二区| 欧美一区二区精品| 中文字幕一区二区三区在线播放| 日本成人在线一区| 日韩欧美国产系列| 中文字幕一区二区三区色视频| 亚洲成人动漫在线观看| 天天色图综合网| 中文字幕一区视频| 色狠狠一区二区三区香蕉| 精品国产亚洲在线| 国产在线国偷精品产拍免费yy| 欧美三级日韩在线| av中文一区二区三区| 成人午夜看片网址| 蜜臀91精品一区二区三区 | 精品国产免费久久| 欧美亚一区二区| 91久久人澡人人添人人爽欧美| 精品久久久久久综合日本欧美| 久久久久久9999| 亚洲一卡二卡三卡四卡无卡久久 | 日韩欧美的一区| 亚洲精品在线观看网站| 午夜电影一区二区三区| 成人一区二区在线观看| 久久综合色之久久综合| 欧美亚洲精品一区| 一区二区三区中文字幕在线观看| 国产一区欧美二区| 国产一区美女在线| 国产精品久久久久久久裸模| 国产成人小视频|