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
echo fully qualified name %~f1
echo drive %~d1
echo path %~p1
echo filename %~n1
echo file extension %~x1
echo short filename %~sn1
echo short file extension %~sx1
echo drive and directory %~dp1
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
drive c:
path \TEMP\
filename longfilename
file extension .long
short filename LONGFI~1
short file extension .LON
drive and directory c:\TEMP\
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).
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).
Reader Comments
Here's a supplementary question: how can you pass parameters to a batch file when it is invoked by dragging an icon onto its shortcut in the GUI? In Win9x, the % variables will contain information about the dragged file, which can be used by the batch file to process it. Very handy. Unfortunately, in Win2K, the % variables are empty, and my batch files fail. Is there a workaround?
Frank Lenk -October 17, 2000
I have successfully used this in many environments, however, when using WinNT 4.0, I have been unable to pass a parameter when using a batch file (followed by a parameter in the user account) to map drives in the logon script file. Please advise if you have any information regarding a method to pass a parameter with the script file to allow mapping to be more easily managed.
Russ -February 26, 2001
How do I grab more than 9 parameters passed to a batch file?
%10 and onwards does not work.
Farid -May 14, 2003
In response to Farid's query you can use the shift command as descibed below...
https://www.microsoft.com/technet/treeview/default.asp?url=/technet/prodtechnol/windowsserver2003/proddocs/datacenter/shift.asp
Bob Cat -August 08, 2003
I am using win98 and in an echo the %1 means files with .sam extension, but %~n1 does not work as file without the extension. what else should I try?
Can you help me?
Thanks
kata -August 29, 2003
Hi, the article is good, but i want to know another question: I already know how pass parameters to another bat file, but can i pass a result (calculated on the second bat file) to the first .bat?
Pablo -September 22, 2003
How do I pass a replaceable parameter to a batch file via a text input box in WIN XP?
In WIN98 I could simply add a ? after .bat in the shortcut box.
In WIN NT I had to put: shell=c:\winnt\system32\command.com/p/e:2048 into a WINNT file called param1.nt then put this file in c:\winnt\system32, pointing the batch file shortcut to param1.nt
Can anyone please tell me how I can make this work in XP? I would be most grateful.
Jeff -November 11, 2003
Regarding InstantDoc #13443
This only applies to Windows 2000/XP or later.
Is there an alternative for Windows 98 SE?
Thanks,
Mike Bytnar -December 03, 2003
How to pass more than 10 parameters in dos batch file.
for example %1 %2.....%9.????next after %9
Manoj Kumar Varshney -April 14, 2004
Parameters #10 and up that are passed to a .BAT file can't be accessed directly with %10%, etc., but they are still there. You just have to shift them into view like this:
:long.bat -how to get extra parms
@echo off
SET ONE______=%1
SET TWO______=%2
SET THREE____=%3
SET FOUR_____=%4
SET FIVE_____=%5
SET SIX______=%6
SET SEVEN____=%7
SET EIGHT____=%8
SET NINE_____=%9
SHIFT
SHIFT
SHIFT
SHIFT
SHIFT
SHIFT
SHIFT
SHIFT
SHIFT
SET TEN______=%1
SET ELEVEN___=%2
SET TWELVE___=%3
SET THIRTEEN_=%4
SET FOURTEEN_=%5
SET FIFTEEN__=%6
SET SIXTEEN__=%7
SET SEVENTEEN=%8
SET EIGHTEEN_=%9
SHIFT
SHIFT
SHIFT
SHIFT
SHIFT
SHIFT
SHIFT
SHIFT
SHIFT
SET NINETEEN_=%1
ECHO ONE______=%ONE______%
ECHO TWO______=%TWO______%
ECHO THREE____=%THREE____%
ECHO FOUR_____=%FOUR_____%
ECHO FIVE_____=%FIVE_____%
ECHO SIX______=%SIX______%
ECHO SEVEN____=%SEVEN____%
ECHO EIGHT____=%EIGHT____%
ECHO NINE_____=%NINE_____%
ECHO TEN______=%TEN______%
ECHO ELEVEN___=%ELEVEN___%
ECHO TWELVE___=%TWELVE___%
ECHO THIRTEEN_=%THIRTEEN_%
ECHO FOURTEEN_=%FOURTEEN_%
ECHO FIFTEEN__=%FIFTEEN__%
ECHO SIXTEEN__=%SIXTEEN__%
ECHO SEVENTEEN=%SEVENTEEN%
ECHO EIGHTEEN_=%EIGHTEEN_%
ECHO NINETEEN_=%NINETEEN_%
SET ONE______=
SET TWO______=
SET THREE____=
SET FOUR_____=
SET FIVE_____=
SET SIX______=
SET SEVEN____=
SET EIGHT____=
SET NINE_____=
SET TEN______=
SET ELEVEN___=
SET TWELVE___=
SET THIRTEEN_=
SET FOURTEEN_=
SET FIFTEEN__=
SET SIXTEEN__=
SET SEVENTEEN=
SET EIGHTEEN_=
SET NINETEEN_=
Anonymous User -November 19, 2004
how to p*** this expression %22 to another batch file with a call statement in w2k? it tries to expand it :(
Anonymous User -January 29, 2005
just what I was looking for! thanks!
Anonymous User -March 19, 2005
I want to read the parameter value from a file how to do it??
Anonymous User -April 04, 2005
you can also use %* for passing all parameters to another batch file.
Anonymous User -May 02, 2005
Yes. There is a solution. Try it yourself.
Anonymous User -June 17, 2005
I hev tried using %~f1 and %~f2 .Please let me know as they ar for fullly qualified path names but if i pass parameters for eg. ".\Logs\Logtrial.log" to them then does it create problem.
Since i am trying to exectue the batch file with %~f1 and passing this path as parametr from command prompt then everything is execute fine but only after execution it displays on the command pronpt "unexpected at this thime"
Please comment on this.
Thanks
Anonymous User -July 11, 2005
i am trying to delete a log file from a batch file using the command del %~f1 which i am accpeting through the command prompt. But when excuted it does not delete the log file mentioned by %~f1.it says "system cannot find the specified path"
Please comment on this.
Free CDs Offer Fundamental Content for IT Pros Are you up to speed on the latest technologies and solutions? Don't miss out on your chance to get up to speed quickly on fundamental, in-depth information on some of the hottest topics in our library of content.
Let Your Users Reset Their Own Passwords: Free Download Try a 30 day free trial of Desktop Authority Password Self-Service – it provides an easy-to-use, robust system for allowing users to reset their own forgotten passwords or locked accounts.
Get Windows IT Pro & Mark Minasi’s Favorite Power Tools Guide Order Windows IT Pro now and get "More of Mark Minasi's Favorite Power Tools"--a in-depth guide to the most useful Windows commands --FREE with your paid order! Subscribe today, and save 58% off the cover price!
Deep Dive into VMware vSphere, eLearning Series Join John Savill to explore the major functionality capabilities of the vSphere virtualization platform, including identification of the changes from ESX 3.5.