What is the reason for the CD /D switch in Windows cmd?
Solution 1
Short answer: Because DOS behaved this way, and cmd
tries to mimic DOS.
Originally, DOS had a 'current director' for each drive, so if you write cd d:\folder
you change the current directory for the D
drive.
You can read more about this here: http://blogs.msdn.com/b/oldnewthing/archive/2010/10/11/10073890.aspx
Solution 2
You have to remember, DOS dates back to before we even had mice to cut and paste text and when screens were 80x25 text. Extra typing, particularly if you had to remember something and type it in later, was extremely painful. Now imagine trying to work on more than one drive. With only one current directory, you'd have to fully specify directories on drives other than the current drive. That would require writing down the paths on the other drives because they wouldn't stay on screen. Ouch.
So instead you could do:
dir a: <- See what dir I need
cd a:foo <- This one
dir a: <- See what file
dir b: <- See what dir I need
cd b:bar <- This one
dir b: <- See what file
a:program b:data <- use them both
Otherwise, it'd be:
dir a: <- See what dir I need
cd a:foo <- This one
dir a: <- See what file (lots of scroll)
dir b:\ <- See what dir I need (scroll)
cd b:\bar <- This one
dir b: <- See what file (lots of scroll)
a:\foo\program b:data <- use them both (had to remember "foo")
Now imagine it's more than one directory deep.
And now, imagine if the program doesn't support subdirectories and you need to pass two paths to it on two different drives.
Comments
-
kefir500 almost 2 years
At first I'd like to say that I do understand the purpose of the
/D
switch for the Windows Command Promptcd
command. I'm just curious why it works this way, and not other. As we all know, it does the following:Use the /D switch to change current drive in addition to changing current directory for a drive.
But every single time I enter (for example)
cd F:
, it's obvious enough that I would like to change the drive. That is why I think this switch is redundant by itself.So what's the point of explicitly setting this switch? Why it isn't implied by default?
-
kefir500 over 8 yearsThank you for your answer! Didn't know that every drive has its own "current directory". Also, I didn't know about the
a:file
functionality, special thanks for this hint :) -
Harry Johnston over 8 yearsFor completeness, note that the "current directory per drive" functionality does still exist at the Win32 level, though the implementation is kind of hacky and not very well documented. Raymond is wrong about it being implemented only in
cmd.exe
, one of his rare mistakes. -
Harry Johnston over 8 yearsStill useful even today, when you're using the command line and multiple drive letters. Though nowadays they are usually drive mappings rather than floppy disks. :-)
-
Mark Segal over 8 years@HarryJohnston Can you please elaborate further? I could only find GetCurrentDirectory and SetCurrentDirectory in WinAPI (setting both the current drive and current directory) - msdn.microsoft.com/en-us/library/windows/desktop/… msdn.microsoft.com/en-us/library/windows/desktop/…
-
Mark Segal over 8 years@HarryJohnston I actually debugged
cmd.exe
and found that when you writecd d:\folder
(and your current drive isc:
) - it doesn't callSetCurrentDirectoryW
, but when you writed:
- it callsSetCurrentDirectoryW
withd:\folder
as a parameter. Are you sure that this functionality is in WinAPI and not incmd.exe
? -
Harry Johnston over 8 yearsHalf and half, come to think of it. The per-drive current directory information is stored in environment variables. (The
set
command hides them from you, but you can see them in the environment block of an application launched from the command line.) If I remember rightly, onlycmd.exe
sets these variables, but (most?) Win32 API functions respect them if you pass them a drive-relative path. Try something likeCreateFile("d:test.txt")
in a command-line application to see what I mean.