June 12th, 2018

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

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.

Author

Raymond has been involved in the evolution of Windows for more than 30 years. In 2003, he began a Web site known as The Old New Thing which has grown in popularity far beyond his wildest imagination, a development which still gives him the heebie-jeebies. The Web site spawned a book, coincidentally also titled The Old New Thing (Addison Wesley 2007). He occasionally appears on the Windows Dev Docs Twitter account to tell stories which convey no useful information.

0 comments

Discussion are closed.

Feedback