Monday, October 26, 2009

Advanced Batch Commands: Command Line Parameters

Starting from Windows NT 4, command line parameters can be retrieved in the following ways.
  • %CmdCmdLine%
    Returns the entire command line as passed to CMD.EXE.  It leaves all delimiters intact, except, in Windows 2000 and later, leading spaces before the first argument.
  • %*
    Returns the remainder of the command line starting from the first argument.  It also leaves all delimiters intact, except, in Windows 2000 and later, leading spaces before the first argument.
  • %~dn
    Returns the drive letter of %n (n can range from 0 to 9) if %n is a valid path or file name (no UNC)
  • %~pn
    Returns the directory of %n if %n is a valid path or file name (no UNC)
  • %~nn
    Returns the file name only of %n if %n is a valid file name
  • %~xn
    Returns the file extension only of %n if %n is a valid file name
  • %~fn
    Returns the fully qualified path of %n if %n is a valid file name or directory
To remove the leading space of %* included by NT 4 use the following commands:

    SET commandline=%*
    IF NOT CMDEXTVERSION 2 SET commandline=%commandline:~1%

No comments:

Post a Comment