20

First I would like to note that I do understand the purpose of the /D switch for the Windows Command Prompt cd command. I'm just curious why it works this way and not otherwise. As we know from the help:

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
  • 4,184
  • 6
  • 42
  • 48
  • 1
    basically because of "evolution". The original `cd` from DOS had no `/d` (probably because nobody ever saw the need for a second drive ;) ) See [this answer](http://stackoverflow.com/a/9483382/2152082) – Stephan Sep 25 '15 at 12:06

2 Answers2

14

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: https://devblogs.microsoft.com/oldnewthing/20101011-00/?p=12563

Saxon Druce
  • 17,406
  • 5
  • 50
  • 71
Mark Segal
  • 5,427
  • 4
  • 31
  • 69
  • 1
    For 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 Sep 25 '15 at 21:10
  • @HarryJohnston Can you please elaborate further? I could only find GetCurrentDirectory and SetCurrentDirectory in WinAPI (setting both the current drive and current directory) - https://msdn.microsoft.com/en-us/library/windows/desktop/aa364934(v=vs.85).aspx https://msdn.microsoft.com/en-us/library/windows/desktop/aa365530(v=vs.85).aspx – Mark Segal Sep 26 '15 at 14:04
  • @HarryJohnston I actually debugged `cmd.exe` and found that when you write `cd d:\folder` (and your current drive is `c:`) - it doesn't call `SetCurrentDirectoryW`, but when you write `d:` - it calls `SetCurrentDirectoryW` with `d:\folder` as a parameter. Are you sure that this functionality is in WinAPI and not in `cmd.exe`? – Mark Segal Sep 26 '15 at 14:51
  • Half 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, only `cmd.exe` *sets* these variables, but (most?) Win32 API functions *respect* them if you pass them a drive-relative path. Try something like `CreateFile("d:test.txt")` in a command-line application to see what I mean. – Harry Johnston Sep 26 '15 at 22:44
8

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.

David Schwartz
  • 179,497
  • 17
  • 214
  • 278
  • Thank 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 :) – kefir500 Sep 25 '15 at 13:23
  • Still 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. :-) – Harry Johnston Sep 25 '15 at 21:11