Stupid cmd.exe tricks: Entering a directory that doesn’t exist, then immediately leaving

Raymond Chen

Raymond

I discovered that cmd.exe lets you enter a nonexistent directory, as long as you leave it before anybody notices.

rem acts like cd C:\Windows
cd C:\doesnt-exist\..\Windows

rem acts like cd C:\Windows
cd C:\really\doesnt-exist\..\..\Windows

rem acts like type C:\Windows\win.ini
type C:\doesnt-exist\..\Windows\win.ini

This is handy if you have a full path to a file on the clipboard and you want to access the parent directory. For example, to chdir into the parent directory, you can type cd, a space, and then paste the full path, and then append \.. and hit Enter.

rem suppose clipboard contains C:\directory\with\file.txt
rem The next line acts like cd C:\directory\with
cd C:\directory\with\file.txt\..

This trick works because cmd.exe does some path simplification before calling into the file system. It sees the .. and says, “Oh, I can do that myself!” and uses it to counteract the previous directory. The previous directory is never accessed, so the command processor doesn’t notice that it never existed.

Raymond Chen
Raymond Chen

Follow Raymond