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 ...
随机推荐
- HW5.21
import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner i ...
- 【转】科普Spark,Spark是什么,如何使用Spark
本博文是转自如下链接,为了方便自己查阅学习和他人交流.感谢原博主的提供! http://www.aboutyun.com/thread-6849-1-1.html http://www.aboutyu ...
- clock_t与time_t的区别及联系
clock_t <ctime> Clock type Type capable of representing clock tick counts and support arithmet ...
- JQuery简单实现锚点链接的平滑滚动
在平时的项目中,我们经常需要一些特效链接,如果使效果进一步加强,我们可以使点击锚点链接平滑滚动到锚点,下面就来给大家讲解下如何使用jQuery来实现. 一般使用锚点来跳转到页面指定位置的时候,会生 ...
- 数字信号处理与音频处理(使用Audition)
前一阵子由于考博学习须要,看了<数字信号处理>,之前一直不清除这门课的理论在哪里应用比較广泛. 这次正巧用Audition处理了一段音频,猛然发现<数字信号处理>这门课还是很实 ...
- excel重复数据
=COUNTIF(H:H,H1)>1
- cocos2dx js文件加密为jsc文件
发布产品,脚本代码是必须要加密的 偶尔会出现编译后的jsc无法运行,或者某些jsb自定义的函数找不到, 最好将require("jsb.js")的全部内容整合到一个文件,然后编译j ...
- 数据库Mysql性能优化
http://www.cnblogs.com/itxiongwei/p/5533995.html
- ACE的安装方法
ACE的安装方法 下载ACE --1 ACE的主页以及下载链接 http://www.cs.wustl.edu/~schmidt/ACE.html 安装ACE --1 将ACE-5.5.tar.g ...
- Cocos-2d 坐标系及其坐标转换
Cocos-2d中,涉及到4种坐标系: GL坐标系Cocos2D以OpenglES为图形库,所以它使用OpenglES坐标系.GL坐标系原点在屏幕左下角,x轴向右,y轴向上. 屏幕坐标系苹果的Quar ...