Not sure about the dos tag. Note this is an extension of cmd.exe (Win 2000 and later).
–
schnaaderFeb 17 '11 at 20:32

3

I added cmd and cmd.exe tags... If someone wants to remove the dos tag, I would probably agree, but a lot of people associate "dos" and cmd.exe, even though cmd.exe has gotten some enhancements since dos.
–
Andy WhiteFeb 17 '11 at 21:07

7 Answers
7

In addition, substitution of FOR
variable references has been enhanced.
You can now use the following optional
syntax:

%~I - expands %I removing any surrounding quotes (")
%~fI - expands %I to a fully qualified path name
%~dI - expands %I to a drive letter only
%~pI - expands %I to a path only
%~nI - expands %I to a file name only
%~xI - expands %I to a file extension only
%~sI - expanded path contains short names only
%~aI - expands %I to file attributes of file
%~tI - expands %I to date/time of file
%~zI - expands %I to size of file
%~$PATH:I - searches the directories listed in the PATH
environment variable and expands %I 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:

%~dpI - expands %I to a drive letter and path only
%~nxI - expands %I to a file name and extension only
%~fsI - expands %I to a full path name with short names only
%~dp$PATH:I - searches the directories listed in the PATH
environment variable for %I and expands to the
drive letter and path of the first one found.
%~ftzaI - expands %I to a DIR like output line

In the above examples %I and PATH can
be replaced by other valid values.
The %~ syntax is terminated by a valid
FOR variable name. Picking upper case
variable names like %I makes it more
readable and avoids confusion with the
modifiers, which are not case
sensitive.

There are different letters you can use like f for "full path name", d for drive letter, p for path, and they can be combined. %~ is the beginning for each of those sequences and a number I denotes it works on the parameter %I (where %0 is the complete name of the batch file, just like you assumed).

I don't know about anyone else, but I get "The following usage of the path operator in batch-parameter substitution is invalid. For valid formats type CALL /? or FOR /?. The syntax of the command is incorrect." when using I. I have to use 0 instead of I. I don't know if I've missed something or whatever, but just letting people know.
–
mythofechelonJun 10 '12 at 15:29

The variable %0 in a batch script is set to the name of the executing batch file. The ~dp special syntax between the % and the 0 basically says to expand the variable %0 to show the drive letter and path, which gives you the current directory containing the batch file!

The %~dp0 Variable

The %~dp0 (that’s a zero) variable when referenced within a Windows
batch file will expand to the drive letter and path of that batch
file.

The variables %0-%9 refer to the command line parameters of the batch
file. %1-%9 refer to command line arguments after the batch file name.
%0 refers to the batch file itself.

If you follow the percent character (%) with a tilde character (~),
you can insert a modifier(s) before the parameter number to alter the
way the variable is expanded. The d modifier expands to the drive
letter and the p modifier expands to the path of the parameter.

Example: Let’s say you have a directory on C: called bat_files, and
in that directory is a file called example.bat. In this case, %~dp0
(combining the d and p modifiers) will expand to C:\bat_files.

Another tip that would help a lot is that to set the current directory to a different drive one would have to use %~d0 first, then cd %~dp0. This will change the directory to the batch file's drive, then change to its folder.

Alternatively, for #oneLinerLovers, as @Omni pointed out in the comments cd /d %~dp0 will change both the drive and directory :)

cd has a built in parameter that will switch both the drive and the folder: cd /d %~dp0
–
OmnipotentEntityOct 4 '12 at 19:42

1

You are right and the /d argument is helpful, but it deserves to be posted as a candidate alternative answer and not a comment to this one. Thank you for pointing this out though, I didn't know about it :)
–
Marvin ThobejaneOct 12 '12 at 11:16

1

you could also use ''pushd %~dp0'' to switch drive and folder
–
fmueckeOct 25 '14 at 19:20

+1 very useful. The if statement checks if a backslash is present and removes the backslash if necessary. The negative 1 in the condition extracts the last character, and the expression in the set statement takes the path string minus the last character (which is the backslash).
–
MattOct 16 '12 at 11:59

for more useful variable combinations (also listed in previous response) from the CMD prompt execute: HELP FOR
which contains this snippet

The modifiers can be combined to get compound results:

%~dpI - expands %I to a drive letter and path only
%~nxI - expands %I to a file name and extension only
%~fsI - expands %I to a full path name with short names only
%~dp$PATH:I - searches the directories listed in the PATH
environment variable for %I and expands to the
drive letter and path of the first one found.
%~ftzaI - expands %I to a DIR like output line