Batch file Functions
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的更多相关文章
- Launch a Batch File With Windows Installer
Quote from: http://flexerasoftware.force.com/articles/en_US/HOWTO/Q111515 Synopsis This article desc ...
- Batch File Rename Utility(文件批量改名软件) 1.1.4231
软件名称: Batch File Rename Utility(文件批量改名软件) 1.1.4231.23098 软件语言: 英文 授权方式: 免费软件 运行环境: Win7 / Vista / Wi ...
- Sublime Text 3 调用cmd运行c、java、python、batch file
一.调用cmd运行c(首先复制MinGW到C盘根目录,并添加环境变量) Tools --> Build System --> New Build System 删除所有内容 复制如下代码进 ...
- 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 ...
- 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 ...
- 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. ...
- Windows batch file
Echo off @ECHO OFF echo string to generate the output create a blank line echo . create a file echo ...
- 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 ...
- 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 ...
随机推荐
- Java同步块(synchronized block)使用详解
Java 同步块(synchronized block)用来标记方法或者代码块是同步的.Java同步块用来避免竞争.本文介绍以下内容: Java同步关键字(synchronzied) 实例方法同步 静 ...
- 问题-[DelphiXE7]新建的安桌模拟器运行程序闪退
问题现象:在DelphiXE7中的手机模拟器,每次运行程序,就闪退?问题处理:在 DelphiXE7的目录中,有一个AVD文件夹,删除后就好了.
- weapon制作武器
weapon制作武器 (weapon.pas/c/cpp) 解题报告 制作武器weapon.pas/c/cpp) 背景 WZland的紧急避难所很快就建好了,WZland的居民们陆续地来到这个紧急避难 ...
- java反射与代理模式
流程age: 饭前洗手----> 吃饭 --->饭后洗碗 //吃饭 public interface Dinner { //吃饭方法 public void haveDinner(); } ...
- SVN的配置与调试
- [置顶] mmog游戏开发之业务篇
这周不是很忙,因为我们的游戏开发了近一年,由于公司的业务调整,在游戏开第二服的时候,老板果断的把项目停到了. 感觉超级的不爽啊.因为这个游戏项目像我的孩子一样和我一样成长,里边的大概的业务逻辑都是偶实 ...
- C#隐式类型
隐式类型 using System; using System.Collections.Generic; using System.Linq; using System.Text; using Sys ...
- SVN源代码泄露
1. 联想b2b站SVN源代码泄露 开启Firefox,输入:b2b.thinkworldshop.com.cn/.svn/entries Webserver返回如下信息:
- 通过NSURLProtocol拦截HTTP转HTTPS来整合SPDY的记录
众所周知,iOS 9.0之后苹果引入ATS限制,苹果也推荐尽量不要使用HTTP通讯了,毕竟是很不安全的.而国内各个有(wu)节操的运营商也会经常篡改请求HTTP请求.所以如果可能,在不影响性能的情况下 ...
- 自己动手,丰衣足食!一大波各式各样的ImageView来袭!
工作略忙,一直想自己打造一个开源控件却苦于没有时间,可是这种事情如果不动手就会一直拖下去,于是最近抽时间做了个简单的自定义形状的ImageView控件. 时间紧迫,目前仅支持正六边形.圆形.菱形.椭圆 ...