Quote from: http://ss64.com/nt/syntax-functions.html

Batch file Functions

Packaging up code into a discrete functions, each with a clear purpose is a very common programming technique. Re-using known, tested code, means you can solve problems very quickly by just bolting together a few functions.

The CMD shell does not have any documented support for functions, but you can fake it by passing arguments/parameters to a subroutine and you can use SETLOCAL to control the visibility of variables.

At first glance building a function may look as simple as this:
:myfunct
SETLOCAL
SET _var1=%1
SET _var2="%_var1%--%_var1%--%_var1%"
SET _result=%_var2%
ENDLOCAL

but there is a problem, the ENDLOCAL command will throw away the _result variable and so the function returns nothing.

:myfunct2
SETLOCAL
SET _var1=%1
SET _var2="%_var1%--%_var1%--%_var1%"
ENDLOCAL
SET _result=%_var2%

This version is getting close, but it still fails to return a value, this time because ENDLOCAL will throw away the _var2 variable

The solution to this is to take advantage of the fact that the CMD shell evaluates variables on a line-by-line basis - so placing ENDLOCAL on the same line as the SET statement(s) gives the result we want. This technique is known as 'tunneling' and works for both functions and entire batch scripts:

:myfunct3
SETLOCAL
SET _var1=%1
SET _var2="%_var1%--%_var1%--%_var1%"
ENDLOCAL & SET _result=%_var2%

In examples above there are just 2 local variables (_var1 and _var2) but in practice there could be far more, by turning the script into a function with SETLOCAL and ENDLOCAL we don’t have to worry if any variable names will clash.

In other words you can do this:

@ECHO OFF 
SET _var1=64
SET _var2=123
CALL :myfunct3 Testing
echo _var1 is %_var1%
echo Final result %_result%
goto :eof

:myfunct3
SETLOCAL
SET _var1=%1
SET _var2="%_var1%--%_var1%--%_var1%"
ENDLOCAL & SET _result=%_var2%

When working with functions it can be useful to use Filename Parameter Extensions against the function name, %0 will contain the call label, %nx0 the file name, see Rob Hubbards blog for an example. Note that if you have two scripts one calling another, this will not reveal the location of the 'calling' script.

“Cats are intended to teach us that not everything in nature has a function” ~ Garrison Keillor

Batch file Functions的更多相关文章

  1. Launch a Batch File With Windows Installer

    Quote from: http://flexerasoftware.force.com/articles/en_US/HOWTO/Q111515 Synopsis This article desc ...

  2. Batch File Rename Utility(文件批量改名软件) 1.1.4231

    软件名称: Batch File Rename Utility(文件批量改名软件) 1.1.4231.23098 软件语言: 英文 授权方式: 免费软件 运行环境: Win7 / Vista / Wi ...

  3. Sublime Text 3 调用cmd运行c、java、python、batch file

    一.调用cmd运行c(首先复制MinGW到C盘根目录,并添加环境变量) Tools --> Build System --> New Build System 删除所有内容 复制如下代码进 ...

  4. run commands in linux shell using batch file

    adb shell as root after device rooted once device rooted, we must perform "su" before we g ...

  5. JDK环境配置: javac is not recognized as an internal or external command, operable program or batch file

    相信大家在配置TestNG的时候,首先都会去确认JDK的安装是否正确,两个命令缺一不可. 打开'cmd' --> 1. 输入'java -version', 返回java home当前路径. j ...

  6. How to run a batch file each time the computer loads Windows

    https://www.computerhope.com/issues/ch000322.htm#:~:text=Press Start%2C type Run%2C and press Enter. ...

  7. Windows batch file

    Echo off @ECHO OFF echo string to generate the output create a blank line echo . create a file echo ...

  8. Cannot generate C# proxy dll with JNI4NET tool, running batch file as trusted assembly?

    From: https://stackoverflow.com/questions/41042368/cannot-generate-c-sharp-proxy-dll-with-jni4net-to ...

  9. How to: Reading an ini config file from a batch file

    Original Link: http://almanachackers.com/blog/2009/12/31/reading-an-ini-config-file-from-a-batch-fil ...

随机推荐

  1. deep learning(1)BP神经网络原理与练习

    具体原理参考如下讲义: 1.神经网络 2.反向传导 3.梯度检验与高级优化 看完材料1和2就可以梳理清楚bp神经网络的基本工作原理,下面通过一个C语言实现的程序来练习这个算法 //Backpropag ...

  2. Fibonacci数列小程序

    Fibonacci数列小程序 问题分析:Fibonacci数列特征是前两项数均为1,从第三项起,前两项的和为第三项的数的数值用公式归纳起来为:f1=f2=1.f1=f1+f2.f2=f1+f2. 程序 ...

  3. Hadoop源码的编译过程详细解读(各版本)

    说在前面的话   重新试多几次.编译过程中会出现下载某个包的时间太久,这是由于连接网站的过程中会出现假死,按ctrl+c,重新运行编译命令. 如果出现缺少了某个文件的情况,则要先清理maven(使用命 ...

  4. Yii Listview 更新及搜索

    更新: http://my.oschina.net/shixiaobao17145/blog/130992 http://www.yiiframework.com/forum/index.php/to ...

  5. poj3101

    不难,结果: 程序: import java.math.*; import java.util.*; public class Main { public static void main(Strin ...

  6. CAS 之 集成RESTful API

    国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html内部邀请码:C8E245J (不写邀请码,没有现金送)国内私 ...

  7. 启用PowerShell Web Access

    Windows PowerShell Web Access(PSWA)是 Windows Server 2012 中的新功能,充当 Windows PowerShell 网关,允许远程计算机基于 We ...

  8. Openstack中间DVR Part1 -- 东西走向的交通处理

    作者:Liping Mao  发表于:2014-07-04 版权声明:能够随意转载.转载时请务必以超链接形式标明文章原始出处和作者信息及本版权声明 在Openstack中L3router会造成流量集中 ...

  9. ThinkPHP中的动态缓存(S方法)和快速缓存(F方法)(转)

    系统默认的缓存方式是采用File方式缓存,我们可以在项目配置文件里面定义其他的缓存方式,例如,修改默认的缓存方式为Xcache(当然,你的环境需要支持Xcache)    对于File方式缓存下的缓存 ...

  10. stack例子

    栈使用在括号匹配中的例子 程序如下: #include<stack> #include<iostream> using namespace std; int main() { ...