打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
How do I pass parameters to a batch file? | Windows Server content from Windows IT Pro
Related: How to parse a batch parameter
A. When you call a batch file, you can enter data after the command that the batch file refers to as %1, %2, etc. For example, in the batch file hello.bat, the following command
@echo hello %1 boy
would output
hello john boy
if you called it as
hello john
The following table outlines how you can modify the passed parameter.
Parameter Description
%1 The normal parameter.
%~f1 Expands %1 to a fully qualified pathname. If you passed only a filename from the current directory, this parameter would also expand to the drive or directory.
%~d1 Extracts the drive letter from %1.
%~p1 Extracts the path from %1.
%~n1 Extracts the filename from %1, without the extension.
%~x1 Extracts the file extension from %1.
%~s1 Changes the n and x options’ meanings to reference the short name. You would therefore use %~sn1 for the short filename and %~sx1 for the short extension.
The following table shows how you can combine some of the parameters.
Parameter Description
%~dp1 Expands %1 to a drive letter and path only.
%~sp1 For short path.
%~nx1 Expands %1 to a filename and extension only.
To see all the parameters in action, put them into the batch file testing.bat, as follows.
@echo off<br>
echo fully qualified name %~f1<br>
echo drive %~d1<br>
echo path %~p1<br>
echo filename %~n1<br>
echo file extension %~x1<br>
echo short filename %~sn1<br>
echo short file extension %~sx1<br>
echo drive and directory %~dp1<br>
echo filename and extension %~nx1
Then, run the file with a long filename. For example, the batch file run on the file c:\temp\longfilename.long would produce the following output.
fully qualified name c:\TEMP\longfilename.long<br>
drive c:<br>
path \TEMP\<br>
filename longfilename<br>
file extension .long<br>
short filename LONGFI~1<br>
short file extension .LON<br>
drive and directory c:\TEMP\<br>
filename and extension longfilename.long
This method also works on the second and subsequent parameters. You simply substitute the parameter for 1 (e.g., %~f2 for the second parameter’s fully qualified path name).
Related: Using URLs in Batch Files
The %0 parameter in a batch file holds information about the file when it runs and indicates which command extensions you can use with the file (e.g., %~dp0 gives the batch file’s drive and path).
Learn more: How many parameters can I pass to batch file? 
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
批处理[bat]完全教程2
批处理文件指南
图片上传及重命名
php常用基本函数总结
aardio 批处理 混合编程
截断在文件包含和上传中的利用
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服