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. HW5.17

    import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner i ...

  2. CSS的一些规范

    请使用简单的语法来链接样式表(type 属性不是必需的): <link rel="stylesheet" href="styles.css"> 短规 ...

  3. 为什么 var_dump("1" == "1e0"); 的结果为true

    今天,同学问我一个问题,如下:var_dump("1" == "1e0"); 的结果是什么. 我的第一反应,答案是false.因为很明显的要比较的是两个字符串, ...

  4. Android执行shell命令

    一.方法 /** * 执行一个shell命令,并返回字符串值 * * @param cmd * 命令名称&参数组成的数组(例如:{"/system/bin/cat", &q ...

  5. java对空格的处理

    public static void main(String[] args) { // TODO Auto-generated method stub String a = " 这个前面有两 ...

  6. contentProvider模板

    package com.example.qunzheng.todolist.provider; import android.content.ContentProvider; import andro ...

  7. Ubuntu设置目录的读写权限(Linux命令chmod 777 dirName)

    更改文件所有者 sudo chown system_username /location_of_files_or_folders 更改文件的权限 鼠标右按钮点击文件/目录 -> 属性 权限 分页 ...

  8. c++制作小游戏--雷电

    用c++实现了一个小游戏--雷电,貌似执行的还不错.贴图和声效也是Duang!Duang!的.整个项目我也会给出下载链接,有兴趣的能够编译执行一下.用到了C++11的新特性,最好是使用vs2013编译 ...

  9. C#中Enum用法小结

      enums枚举是值类型,数据直接存储在栈中,而不是使用引用和真实数据的隔离方式来存储. (1)默认情况下,枚举中的第一个变量被赋值为0,其他的变量的值按定义的顺序来递增(0,12,3...),因此 ...

  10. C# - 集合类 - 集合接口

    本篇将介绍关于集合的接口 这些接口定义了所有与集合有关的类的框架 IEnumerable接口 ns:System.Collections 此接口定义了对集合遍历的方法 一般表示元素序列或集合的类都实现 ...