Categories
DOS & DOS Scripting

Batch Parameter Extensions

These examples use modifiers to modify parameter %1

  • %~f1 Expand %1 to Fully qualified path name
  • %~d1 Expand %1 to Drive letter
  • %~p1 Expand %1 to Path only e.g. \utils\ (ncludes a trailing \) Some commands will interpret this as an escape character.
  • %~n1 Expand %1 to a file Name without file extension C:\utils\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 f, n, s 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.

You can also combine these as shown below:

  • %~dp1 Expand %1 to a drive letter and path
  • %~sp1 Expand %1 to a path shortened to 8.3 characters
  • %~nx1 Expand %2 to a file name and extension only
  • %~dp0 The folder from which the script is being run

Reference:

  • https://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/percent.mspx?mfr=true
  • https://ss64.com/nt/syntax-args.html
  • http://davidthewizard.blogspot.ca/2010/03/better-batch-files-through-command.html
  • http://stackoverflow.com/questions/5034076/what-does-dp0-mean-and-how-does-it-work