Passing arguments to a windows batch file ?

Passing arguments to a windows batch file ?

Asked on October 24, 2018 in Windows.
Add Comment


  • 5 Answer(s)

    java -jar foo.jar %*
    

    To Pass arguments to a windows batch file this code can be used

    Answered on October 24, 2018.
    Add Comment

    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

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

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

    Answered on January 19, 2019.
    Add Comment

    Command Line arguments (Parameters)

    A command line argument (or parameter) is any value passed into a batch script:

    C:> MyScript.cmd January 1234 “Some value”

    Arguments can also be passed to a subroutine with CALL:

    CALL :my_sub 2468

    You can get the value of any argument using a % followed by it’s numerical position on the command line. The first item passed is always %1 the second item is always %2 and so on

    %* in a batch script refers to all the arguments (e.g. %1 %2 %3 %4 %5 …%255)
    only arguments %1 to %9 can be referenced by number.

    Parameter Extensions

    When an argument is used to supply a filename then the following extended syntax can be applied:

    we are using the variable %1 (but this works for any parameter)

    %~f1 Expand %1 to a Fully qualified path name – C:\utils\MyFile.txt

    %~d1
     Expand %1 to a Drive letter only – C:

    %~p1 Expand %1 to a Path only e.g. \utils\ this includes a trailing \ which will be interpreted as an escape character by some commands.

    %~n1 Expand %1 to a file Name without file extension or path – MyFile
    or if only a path is present, with no trailing backslash, the last folder in that path.

    %~x1 Expand %1 to a file eXtension only – .txt

    %~s1 Change the meaning of fns and x to reference the Short 8.3 name (if it exists.)

    %~1   Expand %1 removing any surrounding quotes (“)

    %~a1 Display the file attributes of %1

    %~t1 Display the date/time of %1

    %~z1 Display the file size of %1

    %~$PATH:1 Search the PATH environment variable and expand %1 to the fully qualified name of the first match found.

    The modifiers above can be combined:

    %~dp1 Expand %1 to a drive letter and path only

    %~sp1 Expand %1 to a path shortened to 8.3 characters

    %~nx2 Expand %2 to a file name and extension only

    These parameter/ argument variables are always denoted with a single leading %
    This is unlike regular variables which have both leading and trailing %’s such as %variable%, or FOR command variables which use a single leading % on the command line or a double leading %% when used in a batch file.

    Answered on January 19, 2019.
    Add Comment

     need to pass an ID and a password to a batch file at the time of running rather than hardcoding them into the file.

    Here’s what the command line looks like:

    test.cmd admin [email protected] > test-log.txt
    
    Answered on February 17, 2019.
    Add Comment

    If you want to intelligently handle missing parameters you can do something like:

    IF %1.==. GOTO No1
    IF %2.==. GOTO No2
    ... do stuff...
    GOTO End1
    
    :No1
      ECHO No param 1
    GOTO End1
    :No2
      ECHO No param 2
    GOTO End1
    
    :End1
    
    Answered on February 21, 2019.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.