10

I have mistyped a path and instead of c:\foo.txt wrote c:foo.txt. I expected it to either fail or to resolve to c:\foo.txt, but instead it seems to be resolved to foo.txt in a current user's home folder.

Powershell returns:

PS C:\> [System.IO.Path]::GetFullPath("c:\foo.txt")
c:\foo.txt
PS C:\> [System.IO.Path]::GetFullPath("c:foo.txt")
C:\Users\Administrator\foo.txt
PS C:\> [System.IO.Path]::GetFullPath("g:foo.txt")
G:\foo.txt

Running explorer.exe from commandline and passing it any of the above results in C:\Users\Administrator\Documents to be opened.

I haven't found any documentation of that and I'm utterly confused, please explain the behaviour.

smci
  • 32,567
  • 20
  • 113
  • 146
ya23
  • 14,226
  • 9
  • 46
  • 43
  • You're right, it's not trivial to find documentation on this. I'm still searching myself. This question would fare better on http://superuser.com – Tim Pietzcker May 30 '14 at 13:52
  • @TimPietzcker - See my answer below for the documentation. – weir Jan 03 '18 at 16:01

4 Answers4

8

This is standard DOS/Windows behavior and has always been like this. Open a command line and see:

C:\Users\Tim>d:              # change current drive to d:
D:\>c:                       # change back to c: - back in the same directory
C:\Users\Tim>cd d:\users     # change current directory ON D:
C:\Users\Tim>cd \            # still same directory - backslash leads to top dir
C:\>d:                       # change current drive to d:
D:\Users>                    # notice that we're now in the directory D:\Users

The drive letter always references the current directory of that drive; the (leading) backslash gets you to the top directory.

Tim Pietzcker
  • 328,213
  • 58
  • 503
  • 561
  • This makes sense and works as you describe for commandline. But why my second powershell example (with c:foo.txt) haven't returned c:\foo.txt? Explorer also doesn't follow. Do this: c: | cd\ | mkdir \1\2 | cd 1. Then calling "explorer ." opens c:\1 (fine), but "explorer c:" opens "c:\". "explorer c:2" opens... C:\Users\Administrator\Documents – ya23 May 30 '14 at 15:09
  • 1
    @ya23: `c:foo.txt` refers to `foo.txt` in the current folder on drive `c:` - that's in line with the explanation above, isn't it? – Tim Pietzcker May 30 '14 at 15:46
  • Yes, so it should print `c:\foo.txt`, as the current folder is set to `c:\ ` (as reported by pwd and Get-Location). – ya23 May 30 '14 at 16:01
  • 1
    Your question said it resolved to `foo.txt` in the current working folder. So GetLocation says it's not your current working folder? – Tim Pietzcker May 30 '14 at 16:03
  • Oh, how I see how I confused you. No, it's not my current folder. Will edit the question. – ya23 May 30 '14 at 16:06
8

When you specify a path with a drive letter but no initial backslash, it is typically interpreted as a relative path to the current directory on the specified drive. In particular, this is how ordinary Win32 API file functions will interpret it; therefore, most software which passes unmodified file paths to Win32 file functions will also behave this way.

On my machine, this works as expected in PowerShell, except for one complication:

C:\Users\social>powershell
Windows PowerShell
Copyright (C) 2009 Microsoft Corporation. All rights reserved.

PS C:\Users\social> [System.IO.Path]::GetFullPath("c:foo.txt")
C:\Users\social\foo.txt
PS C:\Users\social> cd \
PS C:\> [System.IO.Path]::GetFullPath("c:foo.txt")
C:\Users\social\foo.txt
PS C:\>

What we see here is that when we change the current directory in PowerShell, it doesn't actually change the current directory. That is, PowerShell has its own idea of what the current directory is, but hasn't bothered to tell Windows about the change. You can confirm this with Process Explorer (which can be downloaded from Microsoft's web site); in the above case, even after using cd, the actual current directory for the PowerShell process was still C:\Users\social.

You also mention Explorer. As near as I can figure, Explorer does its own validation of the path that it is given, which for whatever reason does not permit drive-relative paths. If the path is not considered valid, or does not point to an actual file/folder, the default action is to open the user's Documents folder.

Harry Johnston
  • 35,639
  • 6
  • 68
  • 158
  • Good to know PS doesn't change current dir - never know when it come handy :) What you (and the other answers) describe makes sense. But is there any documentation? – ya23 Jun 03 '14 at 08:46
  • 2
    http://msdn.microsoft.com/en-us/library/windows/desktop/aa365247%28v=vs.85%29.aspx – Harry Johnston Jun 03 '14 at 20:47
5

It uses the current working directory on that drive. Each process "remembers" a current working directory per drive:

 C:\> cd somepath\subdir
 C:\somepath\subdir>  d:
 D:\> dir c:subsubdir       <--  refers to C:\somepath\subdir\subsubdir
wallyk
  • 56,922
  • 16
  • 83
  • 148
1

Here is the documentation/explanation, courtesy of Harry Johnston's comment.

MSDN --> Windows desktop applications --> Develop --> Desktop technologies --> Data Access and Storage --> Local File Systems --> File Management --> About File Management --> Creating, Deleting, and Maintaining Files --> Naming Files, Paths, and Namespaces --> Fully Qualified vs. Relative Paths

For Windows API functions that manipulate files, file names can often be relative to the current directory, while some APIs require a fully qualified path. A file name is relative to the current directory if it does not begin with one of the following:

  • A UNC name of any format, which always start with two backslash characters ("\"). For more information, see the next section.
  • A disk designator with a backslash, for example "C:\" or "d:\".
  • A single backslash, for example, "\directory" or "\file.txt". This is also referred to as an absolute path.

If a file name begins with only a disk designator but not the backslash after the colon, it is interpreted as a relative path to the current directory on the drive with the specified letter. Note that the current directory may or may not be the root directory depending on what it was set to during the most recent "change directory" operation on that disk. Examples of this format are as follows:

  • "C:tmp.txt" refers to a file named "tmp.txt" in the current directory on drive C.
  • "C:tempdir\tmp.txt" refers to a file in a subdirectory to the current directory on drive C.

[...]

weir
  • 4,521
  • 2
  • 29
  • 42