Why do you need to change drives in cmd prompts? - windows

In a cmd prompt, if you want to go from
c:\foo\
to
d:\bar\
You must type
d:
to change drive first, before you can
cd d:\bar\
On Linux, you do not have to do this. Why does cmd unintuitively make you have to do this? Historical? Implementation limitation? By design?
I understand that
cd /d
mimics the Linux behavior, however why is Linux's behavior not default?
As far as I can tell when you
cd anotherDrive:\directory\
it just does nothing, so it's not like the "without /D" usage is reserved for some other semantics.

Having one current working directory per drive allows you to do things like
> cd d:\some\long\path\ # Set the working directory of that other drive
> copy myFile.txt d: # Copy the current file to that drive
> cd .. # Go elsewhere
> copy anotherFile.txt d: # Copy another
I bet this was handy back in the days when you worked with 2 floppy drives.
I don't see any technical reasons for this. Presumably just a design choice of how to work with the prompt.
As PA. mentions you can use the /d switch if you don't like this behaviour.

you don't need to
just read HELP CD
and then try
CD /d d:\bar
the CD default behavior is still there for backwards compatibility with old versions of DOS where the existence of dual diskette bays made it useful.
Newer commands, like pushd for example, make the default behavior to honor drive and path.

Related

How to move up multiple folder

Using VS Code terminal, a certain directory keeps popping up as default :
C:\Users\user\name\folder1\folder2\folder3\folder4
How do move back up with one single command instead of doing multiple "cd ../.."
Much appreciated!
Sorry I am answering for CMD as it was tagged CMD, I'm not sure how this affects VSCode. So, I'm not sure if I should just delete this or if it is actually relevant. Please let me know.
What folder do you want to go to?
Using your Example C:\Users\user\name\folder1\folder2\folder3\folder4
If you would like to go to the root CD \ will do.
If you would like to go up 4 directories CD ..\..\..\.. will do
If you want to go up 4 directories and back down 3 CD ..\..\..\..\folder1\folder2\folder3
If you want to always go to a specific directory on the current disk CD \Path\to\directory
If you want to always go to a specific folder on another disk CD /D DriveLetter:\Path\To\Folder (eg CD /D X:\Path\To\Folder)
In VScode, add the exact folder in Workspace, you want to open terminal from.

CMD won't execute basic CD \

I cannot get CMD or powershell to access anything beyond the first sub directory on a drive without typing the full path. Keeps telling me the system cannot find the specified path.
i.e. This works:
d: cd \dev jobs
and this works:
d: cd \dev jobs\sites
but this doesn't work
d:\dev jobs> cd \sites
I'm running latest windows 10.
c: is my system drive
replicated this proess in the c drive directory and met with the same results
(Please, I'm not asking how to change directories but rather understand why the command line seems to be misbehaving)
Any ideas or suggestions on where to start?
Thank you for your help
Why the unuseful comment vote? That's the right answer.
If you are in d:\dev jobs and you cd sites, it will work. From d:\dev jobs, cd \sites does not work because it is looking for sites in the root folder (ie. d:\sites).
You want to use a relative path there.

What is the reason for the CD /D switch in Windows cmd?

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?
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
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.

Any reason to split up "cd" commands in Windows?

I'm cleaning up some old Windows batch files at work, and keep seeing lines like this:
D:
cd\
cd some\other\path\
is there any reason (compatibility with command.com maybe?) to not just write this as
cd /d d:\some\other\path\
cd doesn't actually change the working drive; it only changes the working directory for that drive. That's why it's broken up that way.
An example might help:
C:\users\david>cd D:\some\path
C:\users\david>
Note the drive hasn't changed.
C:\users\david>D:
D:\some\path>
Now that D: is the "working disk", the working directory is changed to what you specified previously.
As you point out, cd /d will do both. [UPDATE: I must have missed the /d when reading your original post :( -- sorry]
I believe you're correct -- there's no "good" reason not to use the one-liner -- other than compatibility with COMMAND.COM (which I'm pretty sure doesn't support the /d switch). For this reason, I always create my script files with a .cmd extension if they depend on features not supported in COMMAND.COM.
A better alternative IMHO is to use pushd which behaves like cd /d -- and also gives you the ability to go back to wherever you were before (via popd). You can even pushd to a UNC path (\\server\share) and Windows will create a temporary drive letter for you. (Although I only found that feature this morning, and I'm running Win 7 Pro, so I'm not sure if it's available on older versions and/or Home editions.)

What is equivalent to Linux mkdir -p in Windows?

In Linux, mkdir -p creates a folder tree.
What is the equivalent option in Windows to create a folder tree? Is there any?
In Windows, mkdir creates directory trees by default.
mkdir a\b\c
The Windows mkdir does it automatically if command extensions are enabled. They are on just about every box I've ever used but, if they're not, you can create your own script to do it:
#echo off
setlocal enableextensions
md %1
endlocal
Expanding:
Command extensions are an added feature of cmd.exe which allows you to do so much more (at the cost of a little compatibility with earlier incarnations of the batch language).
Windows XP cmd.exe should have these extensions enabled by default but you can configure your box so that they're disabled by default (using "cmd /e:off" as the default processor). If you do that and want to use the extensions, your cmd files must have a setlocal to turn them back on.
The script above could be called md2.cmd and then you would be guaranteed to be able to create multiple directory levels with "md2 a\b\c" without having to worry whether the extensions were enabled.
Almost every one of the cmd scripts I write begins with:
setlocal enableextensions enabledelayedexpansion
to ensure I get as close as possible to the behavior of my beloved bash :-)
For a strange reason when I attempted to create a directory with the following method;
mkdir src/main/java/main/resources
it didn't work, I had to surround the path in double quotes, as shown below;
mkdir "src/main/java/main/resources"
Additionally, unix allows for this;
mkdir -p src/main/java src/main/resources
where two branches will be created as shown below, the equivalent to that on windows is;
mkdir "src/java/resources" "src/main/resources"
src
-----java
-------resources
-----main
-------resources
I hope this helps!
xox
If you want to use forward slashes, just give the directory structure you want within double quotes.
mkdir "org/frame/bu/fed/config"
mkdir by default makes all intermediate directories. Just ensure that you use '\' as the separator.
I just try to create multiple folders on today and it is working!
mkdir "templates" "static/css" "static/js"

Resources