Windows IT Pro is the authoritative and independent resource for windows nt, windows 2000, windows 2003, windows xp. Features a collection of resources and magazines for windows IT professionals.
  
  
  Advanced Search 


January 09, 2000

How do I pass parameters to a batch file?

RSS
Subscribe to Windows IT Pro | See More Systems Administration Articles Here | Reprints | Or get the Monthly Online Pass—only $5.95 a month!

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).

End of Article



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


 See More Comments  1   2 

You must log on before posting a comment.

If you don't have a username & password, please register now.




Top Viewed ArticlesView all articles
Command Prompt Tricks

One reader shares his tip for setting up the command prompt to reflect a remote path. ...

How can I stop and start services from the command line?

...

Microsoft Touts Xbox 360 'Black Friday' Success

Microsoft this week revealed that its Xbox 360 video game console outsold its Sony PlayStation 3 rival 3-to-1 on "Black Friday," which curiously describes the three day weekend after Thanksgiving, a period that unofficially kicks off the holiday selling ...


Windows OSs Whitepapers Why SaaS is the Right Solution for Log Management

Related Events SQL Server 2008 – Can You Wait? | Philadelphia

SQL Server 2008 – Can You Wait? | Atlanta

SQL Server 2008 – Can You Wait? | Chicago

Check out our list of Free Email Newsletters!

Scripting eBooks Keeping Your Business Safe from Attack: Encryption and Certificate Services

Best Practices for Managing Linux and UNIX Servers

Building an Effective Reporting System

Related Scripting Resources Become a VIP member of the Windows IT Pro community!
Get it all with the VIP CD and VIP access. A $500+ value for only $279!

Subscribe to Windows IT Pro!
Solve your toughest technical problems with our experts and access 10,000 + articles online. 30% off

Monthly Online Pass - Only $5.95!
Get instant access to 10,000+ articles from Windows IT Pro Magazine!

TechNet Virtual Labs
Evaluate and test Microsoft's newest products.


Windows IT Pro Home Register FAQ for Windows WinInfo News
Europe Edition About Us Contact Us/Customer Service Media Kit Affiliates / Licensing  
SQL Server Magazine Office & SharePoint Pro Windows Dev Pro IT Job Hound ITTV
IT Library Technology Resource Directory Connected Home Windows Excavator Windows SuperSite 
 
 Windows IT Pro is a Division of Penton Media Inc.
 Copyright © 2008 Penton Media, Inc., All rights reserved. Terms and Use | Privacy Statement | Reprints and Licensing