Knowledge points involved:
1. How to define a function
2. How to pass parameters
3, how to call?
4. How to get the return value
5, Goto:eof and exit/b 0 difference
6. How to handle variables when strings are replaced
7. Concrete examples
Body part:
1. Definition
: functionname REM parameter is%0,%1,...%n REM Todo Goto : EOF
2. Parameters and Calls
Pager : functionname parameter 1 (corresponding to %1), parameter 2 (corresponding to %2),... Parameter n (corresponds to%n)
4, about the return value, directly modify the global variables, batch is no function return value
5, about the difference between goto:eof and exit/b 0
REM goto:eof equivalent to the} end tag of the function, returned to the caller's location REM exit/b 0 ends current cmd, returns ExitCode 0
6. Handling variables in string substitution
1 SetStr_find=%12 SetStr_replace=%23 Setstr_string=%34 5 ::The call command expands its arguments6 ::Call Set "strok=%%str_string:!str_find!=!str_replace!%%"7 8 ::You can also enable variable delay to implement9 ::@echo off&setlocal enabledelayedexpansionTen ::Set "var=!var:%mat%=! One A Set"Strok=!str_string:%1=%2!"
7. Concrete examples
@echo ifSETLOCALenabledelayedexpansiontitle bat function::Author:lovelp::link:http://www.cnblogs.com/lovelp/p/5720046.htmlSetStr1= "This was a old string"SetStr2= "This is a new string"Echo=::=============================================::Set Variable Strok, global, to implement function return valueSetStrok= ""EchoThe function call statement is:Pager : Func1This is a old string,This is a new stringEchofunction end parameters, respectively,%%0,%%1,%%2,...,%%nPager : Func1%str1%,%str2%EchoThe above results are explanatory function parametersEcho.EchoHere's the point.invoking a custom function to implement a string substitutionEcho.Echo Pager : Fun_replaceOld, older,This was a old stringPager : Fun_replaceOld, older,%str1%Echo.Echo↓↓↓↓ call end, display result ↓↓↓↓↓↓↓Echo%strok%::=============================================Exit/b 0: Fun_replace SetStr_find=%1 SetStr_replace=%2 Setstr_string=%3 Echothe string to find is:!str_find! EchoThe final replacement string is:!str_replace! Echothe string body is:%str_string%::The call command expands its arguments ::Call Set "strok=%%str_string:!str_find!=!str_replace!%%" ::You can also enable variable delay to implement @echo off&setlocal enabledelayedexpansion ::Set "var=!var:%mat%=! Set"Strok=!str_string:%1=%2!"GOTO: EOF ::exit/b 0REM(1) After running goto:eof, CMD returns and waits for the next command.REM(2) after running EXIT, CMD will close directly and return to the program that started Cmd.exe or return to "explorer".REM(3) After running exit/b, CMD will close directly and return to the program that started Cmd.exe or return to "explorer".::function name: func1 with colon oh::parameter 1,%0 is the function name itself: Func1::function 2, 3...N is a common parameter.: Func1 Echo%%0Is%0 Echo%%1Is%1 Echo%%2Is%2GOTO: EOF Pause
Batch processing Collection--(2) Custom functions