中文字幕免费精品_亚洲视频自拍_亚洲综合国产激情另类一区_色综合咪咪久久

DOS批處理 函數(shù)定義與用法
來源:易賢網(wǎng) 閱讀:1931 次 日期:2014-10-08 11:55:40
溫馨提示:易賢網(wǎng)小編為您整理了“DOS批處理 函數(shù)定義與用法”,方便廣大網(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:

代碼如下:

: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

更多信息請查看IT技術(shù)專欄

更多信息請查看腳本欄目
易賢網(wǎng)手機(jī)網(wǎng)站地址:DOS批處理 函數(shù)定義與用法
由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢回復(fù)僅供參考,敬請考生以權(quán)威部門公布的正式信息和咨詢?yōu)闇?zhǔn)!

2026上岸·考公考編培訓(xùn)報班

  • 報班類型
  • 姓名
  • 手機(jī)號
  • 驗證碼
關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡要咨詢 | 簡要咨詢須知 | 新媒體/短視頻平臺 | 手機(jī)站點 | 投訴建議
工業(yè)和信息化部備案號:滇ICP備2023014141號-1 云南省教育廳備案號:云教ICP備0901021 滇公網(wǎng)安備53010202001879號 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號
云南網(wǎng)警備案專用圖標(biāo)
聯(lián)系電話:0871-65099533/13759567129 獲取招聘考試信息及咨詢關(guān)注公眾號:hfpxwx
咨詢QQ:1093837350(9:00—18:00)版權(quán)所有:易賢網(wǎng)
云南網(wǎng)警報警專用圖標(biāo)
中文字幕免费精品_亚洲视频自拍_亚洲综合国产激情另类一区_色综合咪咪久久
久久成人亚洲| 开心色5月久久精品| 亚洲在线成人| 欧美久久久久久蜜桃| 激情婷婷久久| 久久久久久午夜| 国模私拍一区二区三区| 久久国产福利| 精品成人a区在线观看| 久久青草欧美一区二区三区| 国模 一区 二区 三区| 久久精品视频播放| 国内精品视频在线播放| 久久久www| 亚洲国产精品传媒在线观看| 欧美成人精品h版在线观看| 亚洲人成欧美中文字幕| 欧美精品偷拍| 在线性视频日韩欧美| 欧美日韩亚洲系列| 亚洲图片你懂的| 国产麻豆午夜三级精品| 久久精品30| 亚洲国产成人精品女人久久久| 媚黑女一区二区| 亚洲精品久久久久中文字幕欢迎你| 欧美国产日韩一区二区| 亚洲美女诱惑| 国产精品久久9| 久久精品五月婷婷| 亚洲欧洲视频| 国产精品久久久久免费a∨大胸| 午夜精品短视频| 国内精品美女av在线播放| 美女视频网站黄色亚洲| 99国内精品久久| 国产精品一二三| 久久亚洲综合色一区二区三区| 亚洲国产中文字幕在线观看| 欧美日韩一区高清| 亚洲欧美一区二区在线观看| 狠狠久久婷婷| 欧美日韩国产精品成人| 午夜精品一区二区三区四区 | 亚洲午夜一区二区三区| 国产精品午夜在线观看| 久久久久久亚洲精品不卡4k岛国| 亚洲高清视频在线观看| 欧美日韩在线第一页| 欧美一区1区三区3区公司| 1769国产精品| 国产精品国产精品| 狼人天天伊人久久| 亚洲视屏在线播放| 影院欧美亚洲| 国产精品丝袜xxxxxxx| 老司机免费视频久久| 夜夜嗨av一区二区三区四季av| 国产精品一区在线观看你懂的| 美国成人毛片| 午夜一区在线| 99精品国产在热久久下载| 国内久久婷婷综合| 欧美三级欧美一级| 麻豆av一区二区三区久久| 亚洲在线成人| 亚洲精品女av网站| 狠狠做深爱婷婷久久综合一区| 欧美日韩一二区| 老**午夜毛片一区二区三区| 亚洲欧美日韩另类| 日韩视频免费| 亚洲成人在线网| 国产亚洲精品久久飘花 | 国产日韩欧美在线播放| 欧美理论电影网| 久久一区精品| 欧美一区二区在线播放| 亚洲午夜精品一区二区| 亚洲全部视频| 亚洲国产精品一区制服丝袜| 国内成人在线| 国产欧美一区二区三区另类精品 | 香蕉成人伊视频在线观看| 亚洲乱码久久| 亚洲国产精品ⅴa在线观看| 国产在线一区二区三区四区| 国产精品婷婷午夜在线观看| 欧美视频一区在线观看| 欧美剧在线观看| 欧美电影资源| 欧美sm重口味系列视频在线观看| 久久久精品tv| 久久久久久夜精品精品免费| 久久精品理论片| 久久精品视频在线播放| 久久福利精品| 久久国产精品电影| 久久成人精品一区二区三区| 久久国产精品毛片| 久久精品亚洲精品| 久久久噜噜噜| 免费看黄裸体一级大秀欧美| 老鸭窝毛片一区二区三区| 久久免费视频这里只有精品| 久久久久九九视频| 久久在线免费| 美女视频一区免费观看| 久久久www成人免费精品| 久久精品一区二区三区中文字幕| 久久精品女人天堂| 久久婷婷综合激情| 免费亚洲婷婷| 欧美另类视频在线| 欧美日韩免费区域视频在线观看| 欧美日韩精品在线| 国产精品久久久久久久久动漫 | 久久久噜噜噜久久中文字幕色伊伊 | 国产一区二区精品久久99| 国产亚洲一区在线播放| 国产一区二区0| 亚洲第一中文字幕在线观看| 亚洲精品在线观| 亚洲视频在线观看三级| 亚洲欧美中文在线视频| 久久精品色图| 欧美国产精品中文字幕| 欧美日韩在线直播| 国产精品久久久久久久久久ktv| 国产精品专区第二| 黄色成人片子| 日韩视频在线播放| 羞羞漫画18久久大片| 久久亚洲精品一区| 欧美精品久久久久a| 欧美日韩色一区| 国产日韩欧美一区| 亚洲精品国产欧美| 亚洲欧美日韩精品在线| 久久色在线观看| 欧美—级在线免费片| 国产精品欧美精品| 在线看片第一页欧美| 9国产精品视频| 久久精品理论片| 欧美日韩一级视频| 国内精品99| 一区二区三区日韩欧美| 久久精品国产一区二区三| 欧美伦理影院| 国语精品中文字幕| 一区二区日本视频| 久久色中文字幕| 国产精品一区二区欧美| 亚洲日本乱码在线观看| 久久99伊人| 国产精品v片在线观看不卡| 一区二区在线视频观看| 亚洲在线成人精品| 欧美电影资源| 韩国v欧美v日本v亚洲v| 亚洲一级片在线观看| 老牛国产精品一区的观看方式| 国产精品白丝av嫩草影院| 亚洲高清毛片| 小处雏高清一区二区三区| 欧美日韩国产三区| 亚洲国产成人av在线| 久久精品国产精品| 国产精品美女久久久浪潮软件 | 欧美日韩国产在线| 亚洲成色www8888| 欧美一区永久视频免费观看| 欧美日韩一区视频| 亚洲激情不卡| 久久久久久久综合| 国产亚洲一二三区| 亚洲欧美在线磁力| 欧美视频在线播放| 日韩午夜中文字幕| 欧美国产精品一区| 在线色欧美三级视频| 久久久久久电影| 国产综合欧美在线看| 销魂美女一区二区三区视频在线| 欧美日韩一区二区三区免费看 | 99热这里只有成人精品国产| 久久嫩草精品久久久精品一| 国产拍揄自揄精品视频麻豆| 亚洲永久精品大片| 欧美性猛交xxxx乱大交蜜桃| 亚洲精品乱码久久久久久日本蜜臀 | 国产欧美日韩亚州综合| 亚洲午夜精品视频| 欧美日韩一级黄| 正在播放日韩| 国产精品久久久久影院色老大 | 红桃视频成人| 久久久一二三| 亚洲大胆av|