Windows IT Pro is the leading independent community for IT professionals deploying Microsoft Windows server and client applications and technologies.
  
  
  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 be a registered user or online subscriber to comment on this article. Please log on before posting a comment. Are you a new visitor? 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. ...

WinInfo Short Takes: 4th of July Special Edition

An often irreverent look at some of the week's other news, including a shortened work week thanks to the 4th of July, expensive Windows 7 pricing, Bing's modest monthly gains, IE 8 heading to work, Steve Jobs back at Apple, and so much more ...

Google Wave Emulates Trends of Changing World

As collaboration continues to increase, the world and how individuals view information is evolving. What does that mean for IT? ...


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

Related Events WinConnections and Microsoft® Exchange Connections

Virtualization Management

Configuration Manager SP1 and R2 Overview

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 Introducing Left-Brain.com, the online IT bookstore
Looking for books, CDs, toolkits, eBooks? Prime your mind at Left-Brain.com

Discover Windows IT Pro eLearning Series!
Clear & detailed technical information and helpful how-to's, all in our trademark no-nonsense format

Test Drive IT Solutions and Get Free Music Downloads
Solve your toughest IT problems with these free downloads and receive 5 free music downloads!


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 DevProConnections IT Job Hound ITTV
IT Library Technology Resource Directory Connected Home asp.netPRO Windows SuperSite 
 
 Windows IT Pro is a Division of Penton Media Inc.
 © 2009 Penton Media, Inc. Terms of Use | Privacy Statement | Reprints and Licensing