Subscribe to Windows IT Pro
January 09, 2000 12:00 AM

How do I pass parameters to a batch file?

Windows IT Pro
InstantDoc ID #13443
Rating: (120)

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

Related Content:

ARTICLE TOOLS

Comments
  • Nawrocki
    2 years ago
    May 24, 2010

    As far as I know the shift batch command predates windows and has been included in all versions of windows. I think some additional options were added in later versions of windows. After the shift command, the next parameter in line becomes the first parameter. In other words that which was %2 before the shift is %1 after the shift. %3 becomes %2 an so on.

    The easiest way is to code a loop for as many parameters as you have and process each one as %1.

    @ECHO OFF
    :Loop
    IF "%1"=="" GOTO Continue

    Here do something with parameter

    SHIFT
    GOTO Loop
    :Continue

  • Anonymous User
    7 years ago
    Jul 12, 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.

  • Anonymous User
    7 years ago
    Jul 11, 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
    7 years ago
    Jun 17, 2005

    Yes. There is a solution. Try it yourself.

  • Anonymous User
    7 years ago
    May 02, 2005

    you can also use %* for passing all parameters to another batch file.

You must log on before posting a comment.

Are you a new visitor? Register Here

advertisement

advertisement

Windows is a trademark of the Microsoft group of companies. Windows IT Pro is used by Penton Media Inc. under license from owner.