Parameters
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的更多相关文章
- 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 ...
- Unity: Passing Constructor Parameters to Resolve
In this tutorial we will go through of couple different ways of using custom constructor parameters ...
- C#编程:SqlCommand.Parameters.Add()方法的参数问题。
在存储过程中添加2个参数 sql语句 例: “update [tablename] username = @username where id=@id” 然后把需要的 command.Paramete ...
- Parameter index out of range (2 > number of parameters, which is 1)
今天在实现一个功能时遇到一个问题,解决了很久.结果是#{}与${}使用错误的原因.但是具体原因还不是很清楚,写此篇总结,知道的可以交流. 具体描述为:通过教师的头衔(1高级讲师2首席讲师)及名称进行模 ...
- java中获取接口(方法)中的参数名字(eclipse设置编译参数)(java8 javac -parameters)
interface接口参数 jdk1.7及以前使用spring功能实现的: 注意: 1.该功能只能获取类的方法的参数名,不能获取接口的方法的参数名. public static void test() ...
- ios AFNetworking 3.0 报错 : *** Assertion failure in -[AFHTTPRequestSerializer requestWithMethod:URLString:parameters:error:],
AFNetWorking[:] *** Assertion failure -- :::] *** Terminating app due to uncaught exception 'NSInter ...
- 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 ...
- How to Change RabbitMQ Queue Parameters in Production?
RabbitMQ does not allow re-declaring a queue with different values of parameters such as durability, ...
- has no parameters and arguments were supplied
这个问题,让Insus.NET花上不少时间与心机. 在项目中,Insus.NET是使用这个逻辑组件: <程序与数据库之间的连接桥梁和逻辑处理>http://www.cnblogs.com/ ...
- ES6函数剩余参数(Rest Parameters)
我们知道JS函数内部有个arguments对象,可以拿到全部实参.现在ES6给我们带来了一个新的对象,可以拿到除开始参数外的参数,即剩余参数(废话好多 O(∩_∩)O~). 这个新的对象和argume ...
随机推荐
- Esper系列(七)数据缓存、外部事件应用(静态方法)
LRU Cache 功能:最近最少使用策略. 数据库查询缓存应用配置: 1 ); 12 } 13 return bean; 14 } 15 } ...
- iOS 设置connect超时
NSLock *theLock; [theLock lock]; int fd, error; struct sockaddr_in addr; ))<) { cout<<" ...
- HTTP/2 常见问题回答
常见问题 为什么修订HTTP? HTTP/1.1已经很好的服务Web超过15个年头,但它的劣势开始显现. 载入一个Web页面比之前占用更多的资源(详情可见HTTP压缩页大小统计),有效的载入这些资源很 ...
- WPF线程获取UI线程
WPF中只能是UI线程才可以改变UI控件相关,当采用多线程工作时,可用以下代码获取 UI线程进行操作: App.Current.Dispatcher.Invoke((Action)delegate() ...
- 北京Uber优步司机奖励政策(2月3日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- [USACO08JAN]电话线Telephone Lines
多年以后,笨笨长大了,成为了电话线布置师.由于地震使得某市的电话线全部损坏,笨笨是负责接到震中市的负责人.该市周围分布着N(1<=N<=1000)根据1……n顺序编号的废弃的电话线杆,任意 ...
- IOS获取物理尺寸中7Plus中获取的是7的物理尺寸
IOS获取物理尺寸中7Plus中获取的是7的物理尺寸: 在开发调试过程中我的7Plus手机获取[uiscreen mainscreen].bounds为750 .1334. 解决方案:在手机中的显示 ...
- C# webkit.net的简单使用
webkit.net 0.5下载地址https://sourceforge.net/projects/webkitdotnet/files/WebKit%20.NET%200.x/0.5/WebK ...
- Javascript模板引擎handlebars使用实例及技巧
转:http://rfyiamcool.blog.51cto.com/1030776/1278620 我们在开发的时候针对DOM操作,用简单的JS应用来说不成问题,但如果你对视图的每次更新都需要对我文 ...
- Android软键盘调用及隐藏,以及获得点击软键盘输入的字母信息
在Android提供的EditText中单击的时候,会自动的弹出软键盘,其实对于软键盘的控制我们可以通过InputMethodManager这个类来实现.我们需要控制软键盘的方式就是两种一个是像Edi ...