1

Given the batch file foo.bat

@echo %~dp1

In the current directory c:\subdir ... if one executes

c:\subdir>foo c:

A Windows 7 command prompt window echoes C:\

I would expect C:\subdir\

Why is C:\ produced when I am referring to the current directory (not the root)?

Dir seems to handle it properly... again given one is in c:\subdir:

c:\subdir>dir c:/b/s

yields C:\subdir\foo.bat

Paul Houle
  • 735
  • 9
  • 17
  • 3
    Because Microsoft made it like that. ;-) I believe the main purpose is that you can append all the parts `~d`, `~p`, `~n`, `~x` to get the full path, without having to insert any extra `\ `characters. When a relative path is given, the resulting path is related to the current working directory; if a drive and a relative path is given, the resulting path is related to the current directory of that drive. Note that `C:` (relative path pointing to the current directory of the drive) is not the same as `C:\ `(absolute path pointing to the root directory of the drive). I can't reproduce your `C:.`. – aschipfl Oct 11 '17 at 09:12
  • 2
    `%CD%` expands to the current directory, not `%~dp1`, which expands to the directory and path of the first argument/parameter, _(if one exists)_. Oh and in Windows it's called the cmd.exe or Command prompt window, there is no DOS. – Compo Oct 11 '17 at 09:59
  • 1
    CMD gets the full path for "C:" via [`GetFullPathName`](https://msdn.microsoft.com/en-us/library/aa364963), which expands to the working directory on "C:", but without a trailing backslash. CMD uses the optional `lpFilePart` argument. Note that the claim in the docs is misleading. The file system isn't checked; `lpFilePart` simply receives zero when the path ends in a trailing backslash. So `%~dp1` always omits the last directory when `%1` is the path to a directory that doesn't end in a trailing backslash, as occurs for "C:". I assume you have a typo with "c:\.". It should work with "C:.\". – Eryk Sun Oct 11 '17 at 13:21
  • Extra note: `%~dp0`, `%~dp1`, ... always end with directory separator ``\`` as in my opinion is strongly recommended for paths in any application. Knowing that a __path__ always ends with a backlash makes it easy to concatenate the path with one more path, a directory name or a file name. `%CD%` expands to a name of current directory with full path __usually__ not ending with a backslash. But if current directory is the root directory of a drive, `%CD%` expands to drive letter, colon and a backslash. And that must be taken into account with an additional __IF__ in batch files on using `%CD%`. – Mofi Oct 11 '17 at 17:22
  • It's not strictly relevant to talk about `%CD%` here. If "C:" is passed as the parameter, the user wants the working directory on drive C:, not the current working directory of the process itself. The problem of ambiguity about what constitutes a [n]ame (i.e. from `lpFilePart` mentioned in the previous comment) versus the [p]ath is resolved by passing "C:.\", for which in this case `%~dp1` should expand to "C:\subdir\", even if we run the script with the working directory on drive Z:. "C:.\" looks a bit ugly, but we have to be explicit. – Eryk Sun Oct 11 '17 at 17:41
  • @Mofi, the Windows API is inconsistent about trailing backslashes on the path to a directory. Look no farther than `GetFullPathName`, which requires a trailing backslash to indicate a directory for `lpFilePart`, but doesn't add one when expanding "C:" to the working directory on drive C:. – Eryk Sun Oct 11 '17 at 17:45
  • See https://stackoverflow.com/questions/5034076/what-does-dp0-mean-and-how-does-it-work – Ceddaerrix Jul 17 '20 at 13:07

0 Answers0