在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
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. The benefits behind functions are: 1.Keep the main script clean Create a Function - What is a function 复制代码 代码如下: :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 Example - Calling a Function - An Example showing how it works 1.The main script: starting at line 1 ending with a GOTO:EOF command that terminates the script. 复制代码 代码如下: @echo off echo.going to execute myDosFunc call:myDosFunc echo.returned from myDosFunc echo.&pause&goto:eof ::-------------------------------------------------------- :myDosFunc - here starts my function identified by it`s label 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. 复制代码 代码如下: @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 ::-------------------------------------------------------- :myDosFunc - here starts my function identified by it's label 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 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. 复制代码 代码如下: 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. 复制代码 代码如下: 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.&pause&goto:eof
:myGetFunc - passing a variable by reference 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 复制代码 代码如下: @echo off set "aStr=Expect no changed, even if used in function" echo.&pause&goto:eof ::-------------------------------------------------------- :myGetFunc - passing a variable by reference 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" echo.&pause&goto:eof ::-------------------------------------------------------- :myGetFunc - passing a variable by reference :myGetFunc2 - passing a variable by reference 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!!! 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 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. 复制代码 代码如下: @echo off set "fst=0" echo.&pause&goto:eof
:myFibo -- calculate recursively the next Fibonacci number greater or equal to a limit 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 |
请发表评论