Skip to content

Instantly share code, notes, and snippets.

@balmyBanzai
Last active January 5, 2017 22:04
Show Gist options
  • Save balmyBanzai/da74efb3c2b0af29609b2bdeba22bf56 to your computer and use it in GitHub Desktop.
Save balmyBanzai/da74efb3c2b0af29609b2bdeba22bf56 to your computer and use it in GitHub Desktop.
Parameter Expansion tips
All of These tips were taken directly from Jon Galloway's "Top 10 DOS Batch tips (Yes, DOS Batch...)" article from Monday, November 20, 2006 at https://weblogs.asp.net/jongalloway/top-10-dos-batch-tips-yes-dos-batch
This gist is for posterity in the event of the site's removal or expiration

batchparams.bat

@echo off
echo %%~1     =      %~1 
echo %%~f1     =      %~f1
echo %%~d1     =      %~d1
echo %%~p1     =      %~p1
echo %%~n1     =      %~n1
echo %%~x1     =      %~x1
echo %%~s1     =      %~s1
echo %%~a1     =      %~a1
echo %%~t1     =      %~t1
echo %%~z1     =      %~z1
echo %%~$PATHATH:1     =      %~$PATHATH:1
echo %%~dp1     =      %~dp1
echo %%~nx1     =      %~nx1
echo %%~dp$PATH:1     =      %~dp$PATH:1
echo %%~ftza1     =      %~ftza1

Example output

REM Now we'll call it, passing in "C:\Windows\Notepad.exe" as a parameter:
REM C:\Temp>batchparams.bat c:\windows\notepad.exe
%~1     =      c:\windows\notepad.exe
%~f1     =      c:\WINDOWS\NOTEPAD.EXE
%~d1     =      c:
%~p1     =      \WINDOWS\
%~n1     =      NOTEPAD
%~x1     =      .EXE
%~s1     =      c:\WINDOWS\NOTEPAD.EXE
%~a1     =      --a------
%~t1     =      08/25/2005 01:50 AM
%~z1     =      17920
%~$PATHATH:1     =
%~dp1     =      c:\WINDOWS\
%~nx1     =      NOTEPAD.EXE
%~dp$PATH:1     =      c:\WINDOWS\
%~ftza1     =      --a------ 08/25/2005 01:50 AM 17920 c:\WINDOWS\NOTEPAD.EXE

Explanation

%~1 - expands %1 removing any surrounding quotes (")

%~f1 - expands %1 to a fully qualified path name

%~d1 - expands %1 to a drive letter only

%~p1 - expands %1 to a path only

%~n1 - expands %1 to a file name only

%~x1 - expands %1 to a file extension only

%~s1 - expanded path contains short names only

%~a1 - expands %1 to file attributes

%~t1 - expands %1 to date/time of file

%~z1 - expands %1 to size of file

%~$PATH:1 - searches the directories listed in the PATH environment variable and expands %1 to the fully qualified name of the first one found. If the environment variable name is not defined or the file is not found by the search, then this modifier expands to the empty string

The modifiers can be combined to get compound results:

%~dp1 - expands %1 to a drive letter and path only

%~nx1 - expands %1 to a file name and extension only

%~dp$PATH:1 - searches the directories listed in the PATH environment variable for %1 and expands to the drive letter and path of the first one found.

%~ftza1 - expands %1 to a DIR like output line

In the above examples %1 and PATH can be replaced by other valid values. The %~ syntax is terminated by a valid argument number. The %~ modifiers may not be used with %*

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment