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. ssl error rx record too long

  2. Learning JavaScript Design Patterns The Constructor Pattern

    In classical object-oriented programming languages, a constructor is a special method used to initia ...

  3. requirejs 打包参数

    https://github.com/requirejs/r.js/blob/master/build/example.build.js

  4. 开发日志_Jan.8.2017

    这两天继续着手开发碰撞部分. 主要工作是写碰撞类和运动线程类.碰撞主要在于算法,运动线程只要管理好就行了. 之前碰撞测试中(即还未添加完整碰撞算法时)遇到各种bug,疑似机器人和小球的定位点不明所造成 ...

  5. linux中删除目录

    在linux中删除一个目录很简单,很多人还是习惯用rmdir,不过一旦目录非空,就陷入深深的苦恼之中,现在使用rm -rf命令即可解决. 直接rm就可以了,不过要加两个参数-rf 即:rm -rf 目 ...

  6. UIImageC处理

    UIImageC处理 1.等比缩放 - (UIImage *) scaleImage:(UIImage *)image toScale:(float)scaleSize { UIGraphicsBeg ...

  7. strlen和sizeof的差别

    一.sizeof     sizeof(...)是运算符,而不是一个函数.     一个简单的样例: int a; cout<<sizeof a<<endl;     在头文件 ...

  8. Nginx/Apache图片缩略图技术

    1,目的 2,使用方式 3,Nginx + Linux 缩略图实现 3.1,原理 3.2,nginx配置实现 3.3,例子 4,Apache + Windows缩略图实现 4.1,环境 4.2,原理 ...

  9. Mac OS X 如何设置默认浏览器

    有时候我们不希望在 Mac 中点击任何连接都打开的是 Safari,这需要修改默认浏览器设置,在 Mac OS X 中如何设置默认浏览器呢? 打开 Safari 的偏好设置,在「通用」选项卡中有「默认 ...

  10. 【转】C/C++中的日期和时间 TIME_T与STRUCT TM转换——2013-08-25 16

    http://www.cnblogs.com/Wiseman/archive/2005/10/24/260576.html 摘要: 本文从介绍基础概念入手,探讨了在C/C++中对日期和时间操作所用到的 ...