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

Parameters

A parameter (or argument) is any value passed into a batch script:

C:> MyScript.cmd January 1234 "Some value"

Parameters may also be passed to a subroutine with CALL:

CALL :my_sub 2468

You can get the value of any parameter using a % followed by it's numerical position on the command line. The first item passed is always %1 the second item is always %2 and so on

%* in a batch script refers to all the arguments (e.g. %1 %2 %3 %4 %5 ...%255)

Parameter Extensions

When a parameter is used to supply a filename then the following extended syntax can be applied:

we are using the variable %1 (but this works for any parameter)

%~f1 Expand %1 to a Fully qualified path name - C:\utils\MyFile.txt

%~d1 Expand %1 to a Drive letter only - C:

%~p1 Expand %1 to a Path only e.g. \utils\ this includes a trailing \ which may be interpreted as an escape character by some commands.

%~n1 Expand %1 to a file Name, or if only a path is present (with no trailing backslash\) - the last folder in that path

%~x1 Expand %1 to a file eXtension only - .txt

%~s1 Change the meaning of f, n and x to reference the Short name (see note below)

%~1   Expand %1 removing any surrounding quotes (")

%~a1 Display the file attributes of %1

%~t1 Display the date/time of %1

%~z1 Display the file size of %1

%~$PATH:1 Search the PATH environment variable and expand %1 to the fully qualified name of the first match found.

The modifiers above can be combined:

%~dp1 Expand %1 to a drive letter and path only

%~nx2 Expand %2 to a file name and extension only

When writing batch scripts it's a good idea to store these values in a variable SET _LogFile=%~dp1, the rest of the script can then refer to the easy-to-read variable name %_LogFile% This will also make life easier if you later need to change around the order of the parameters.

Passing by Reference

In addition to passing numeric or string values on the command line, it is also possible to pass a variable name and then use the variable to transfer data between scripts or subroutines. Passing by reference is a slightly more advanced technique but can be particularly useful when the string contains characters that are CMD delimiters or quotes.

Links relative to the Batch Script

You can get the pathname of the batch script itself with %0, parameter extensions can be applied to this so %~dp0 will return the Drive and Path to the batch script e.g. W:\scripts\ and %~f0 will return the full pathname W:\scripts\mybatch.cmd

You can refer to other files in the same folder as the batch script by using this syntax:

  CALL %0\..\SecondBatch.cmd

This can even be used in a subroutine, Echo %0 will give the call label but, echo "%~nx0" will give you the filename of the batch script.

When the %0 variable is expanded, the result is enclosed in quotation marks.

Bug when using ~s for short file/folder names

There is a bug involving the ~s option, the displayed output may be wrong if the current directory name is not the same as the short (8.3) name of the directory.
A workaround is to run command.com /c rem which will change the current directory to 8.3, details here.

Use %~a1 to display the Extended Attributes of a file.

FOR's %%~aI recognizes 9 NTFS file attributes. The expansion of a file attribute produces a series of 9 dashes, with each recognized attribute replacing a dash with a letter. A file with no recognized attributes or with none set will expand to 9 dashes like this: ---------

 Attribute                    Expansion
FILE_ATTRIBUTE_DIRECTORY d--------
FILE_ATTRIBUTE_READONLY -r-------
FILE_ATTRIBUTE_ARCHIVE --a------
FILE_ATTRIBUTE_HIDDEN ---h-----
FILE_ATTRIBUTE_SYSTEM ----s----
FILE_ATTRIBUTE_COMPRESSED -----c---
FILE_ATTRIBUTE_OFFLINE ------o--
FILE_ATTRIBUTE_TEMPORARY -------t-
FILE_ATTRIBUTE_REPARSE_POINT --------l
FILE_ATTRIBUTE_NORMAL ---------

Other NTFS attributes not recognised by %%~aI can be read using FSUTIL usn command
FILE_ATTRIBUTE_ENCRYPTED 
FILE_ATTRIBUTE_NOT_CONTENT_INDEXED 
FILE_ATTRIBUTE_SPARSE_FILE

Example: Expansion of a file with the Hidden and System attributes:
---hs----

FOR parameters

The FOR command creates parameter variables which are identified with a letter rather than a number (e.g. %%G). 
The Parameter Expansions described above can also be applied to these. 
To avoid confusion between the two sets of letters you may wish to avoid using the letters (a, d, f, n, p, s, t, x, z) as FOR parameters or just choose a FOR parameter letter thats UPPER case.
So for example in a reference like %%~fG the %%G is the FOR parameter, and the ~f is the Parameter Expansion.

Examples:

Pass parameters from one batch to another:

   MyBatch.cmd SMITH 100

Or as part of a CALL :

   CALL MyBatch.cmd SMITH 100

Passing values from one part of a script to another:

   :: Using CALL to jump to a subroutine
CALL :s_staff SMITH 100 :: Calling a subroutine from a FOR command
FOR /F %%G IN ('DIR /b *.*') DO call :s_subroutine %%G

“A gift is pure when it is given from the heart to the right person at the right time and at the right place, and when we expect nothing in return” ~ The Bhagavad Gita

Parameters的更多相关文章

  1. ORA-01078: failure in processing system parameters & LRM-00109: could not open parameter file

    安装了Oracle 12C后,启动数据库的过程中出现如下错误 SQL> startup ORA-01078: failure in processing system parameters LR ...

  2. Unity: Passing Constructor Parameters to Resolve

    In this tutorial we will go through of couple different ways of using custom constructor parameters ...

  3. C#编程:SqlCommand.Parameters.Add()方法的参数问题。

    在存储过程中添加2个参数 sql语句 例: “update [tablename] username = @username where id=@id” 然后把需要的 command.Paramete ...

  4. Parameter index out of range (2 > number of parameters, which is 1)

    今天在实现一个功能时遇到一个问题,解决了很久.结果是#{}与${}使用错误的原因.但是具体原因还不是很清楚,写此篇总结,知道的可以交流. 具体描述为:通过教师的头衔(1高级讲师2首席讲师)及名称进行模 ...

  5. java中获取接口(方法)中的参数名字(eclipse设置编译参数)(java8 javac -parameters)

    interface接口参数 jdk1.7及以前使用spring功能实现的: 注意: 1.该功能只能获取类的方法的参数名,不能获取接口的方法的参数名. public static void test() ...

  6. ios AFNetworking 3.0 报错 : *** Assertion failure in -[AFHTTPRequestSerializer requestWithMethod:URLString:parameters:error:],

    AFNetWorking[:] *** Assertion failure -- :::] *** Terminating app due to uncaught exception 'NSInter ...

  7. Parameter Passing / Request Parameters in JSF 2.0 (转)

    This Blog is a compilation of various methods of passing Request Parameters in JSF (2.0 +) (1)  f:vi ...

  8. How to Change RabbitMQ Queue Parameters in Production?

    RabbitMQ does not allow re-declaring a queue with different values of parameters such as durability, ...

  9. has no parameters and arguments were supplied

    这个问题,让Insus.NET花上不少时间与心机. 在项目中,Insus.NET是使用这个逻辑组件: <程序与数据库之间的连接桥梁和逻辑处理>http://www.cnblogs.com/ ...

  10. ES6函数剩余参数(Rest Parameters)

    我们知道JS函数内部有个arguments对象,可以拿到全部实参.现在ES6给我们带来了一个新的对象,可以拿到除开始参数外的参数,即剩余参数(废话好多 O(∩_∩)O~). 这个新的对象和argume ...

随机推荐

  1. 【前端】CSS3实现弹出效果

    36氪这个网站上的登录框弹出的时候挺帅气的,想知道它是怎么做的 .. 今天通过问新爷再加上自己琢磨琢磨写出一个小小Demo - 上代码 <!DOCTYPE html> <html&g ...

  2. Java 动态生成 复杂 .doc文件

    阅读目录 1.word 里面调整好排版,包括你想生成的动态部分,还有一些不用生成的规则性的文字 2. 将 word 文档保存为 xml 3.用 Firstobject free XML edito 打 ...

  3. YUV422蓝屏显示输出功能辅助调试

    YUV422蓝屏显示输出功能辅助调试 YUV422有YUYV,YVYU,UYVY,VYUY四种,以下笔者就就以UYVY为例介绍一下数据构成.因为常常要跟视频输入打交道,所以YUV422这种常见的视频信 ...

  4. Nginx重要结构request_t解析之http请求的获取

    请在文章页面明显位置给出原文连接,否则保留追究法律责任的权利. 本文主要参考为<深入理解nginx模块开发与架构解析>一书,处理用户请求部分,是一篇包含作者理解的读书笔记.欢迎指正,讨论. ...

  5. requestFocusFromTouch , requestFocus

    /*if(userNameEditText.isInTouchMode()){ userNameEditText.requestFocusFromTouch(); }else{ userNameEdi ...

  6. careercup-递归和动态规划 9.5

    9.5 编写一个方法,确定某字符串的所有排列组合. 类似leetcode:Permutations 解法: 跟许多递归问题一样,简单构造法非常管用.假设有个字符串S,以字符序列a1a2a...an表示 ...

  7. redis持久化(摘录)

    redis是一个支持持久化的内存数据库,也就是说redis需要经常将内存中的数据同步到磁盘来保证持久化.redis支持两种持久化方式,一种是 Snapshotting(快照)也是默认方式,另一种是Ap ...

  8. freewrap——将tcl/tk脚本转变为可执行文件

     FreeWrap可以把TCL/TK的脚本和二进制文件打包成应用程序,FreeWrap将所有的文件组合成一个单独的可执行文件.     FreeWrap的原理是把脚本和tcl/tk解释器和库文件都打包 ...

  9. 关于IPv6

    App在本地IPv6的测试环境下运行一切正常,结果又是被拒,悲剧原因还是IPv6的问题;求解决方法被拒原因We discovered one or more bugs in your app when ...

  10. 分享4个网址二维码API接口

    说明:把url=后面的网址改成你的,四种任选一.http://pan.baidu.com/share/qrcode?w=150&h=150&url=http://lanyes.org ...