%~dp0

Tuesday, 24 December 2013, 15:53

Tags: CLI Windows

%~dp0 is a very useful command when writing bat files. It will resolve to the folder in which the executing bat file is located. It is also very weird looking, but there is an explanation for the structure of the command. By splitting up the command into its smaller components it will become clear what the function of the different characters is.

  • %0: resolves to the executing bat file
  • %1: resolves to the first argument
  • %~1: resolves to the first argument and removes surrounding quotes
  • %~d0: resolves to the drive of the executing bat file
  • %~p0: resolves to the path of the executing bat file
  • %~dp0: resolves to the folder of the executing bat file

Next to d and p there are other characters that can be used to extract information about the executing bat file or its parameters. A nice overview can be found here.

Now let's take a look at an example that shows the usage of the different components of %~dp0.