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\
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
cddoesn’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:
Note the drive hasn’t changed.
Now that D: is the “working disk”, the working directory is changed to what you specified previously.
As you point out,
cd /dwill do both. [UPDATE: I must have missed the/dwhen 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
/dswitch). For this reason, I always create my script files with a.cmdextension if they depend on features not supported in COMMAND.COM.A better alternative IMHO is to use
pushdwhich behaves likecd /d— and also gives you the ability to go back to wherever you were before (viapopd). You can evenpushdto 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.)