2

I'm working on a Windows machine using cmd and I want to create a new diretory and navigate into it in one command. Under Unix you can do something like the following:

mkdir new_dir && cd $_

How can I achieve that under Windows?

Thanks in advance, have a nice day!

Train
  • 585
  • 1
  • 6
  • 20
  • 2
    `md new_dir & cd new_dir` – aschipfl Jul 14 '18 at 20:39
  • Exactly the same way. `& seperates commands on a line.` `&& executes this command only if previous command's errorlevel is 0.` `|| (not used above) executes this command only if previous command's errorlevel is NOT 0` – CatCat Jul 14 '18 at 20:40
  • Thanks a lot for your answer, but it's not possible to pipe the previous argument? So I don't have to explicitly write the directory name again. Cause under Unix I can do the exact same thing you wrote, but I can also use the argument from the previous command using $_. And I'm searching for a similar solution. – Train Jul 14 '18 at 20:54
  • 1
    `cmd` has no such functionality. – Stephan Jul 14 '18 at 21:18
  • `doskey cd=md $1 $T cd $1` then `cd dog`. See `Doskey /?`. See https://stackoverflow.com/questions/41030190/command-to-run-a-bat-file/41049135#41049135 – CatCat Jul 14 '18 at 22:56

2 Answers2

3

If you're not opposed to using Windows Powershell (you can simply execute powershell in command prompt to enter an instance of the Powershell shell), this is the closest way to do what you are asking.

echo new_dir | %{mkdir $_; cd $_}

Adapted from simonwo's xargs answer.


However, if for certain reasons you are unable to use Powershell, then this is a method to achieve very similar behavior. The issue being simply that you are now using variables instead of redirecting output.

SET "d=new_dir" && call mkdir %^d% && call cd %^d%

Adapted from jeb's oneliner answer.


Similarly, for a slightly more readable input string and portable method (due to the limitations of call) than the above, you can use the following. However, note that you will be moved into a new separate instance of the command line shell.

cmd /v /k "set d=new_dir && mkdir !d! && cd !d!"

The /v argument enables delayed variable expansion, and the /k argument carries out the command by the string but remains without terminating.

Adapted from Blogbeard's oneliner answer.

Shenk
  • 352
  • 4
  • 12
  • Thank you for that detailed answer. I'm bonded with the CMD. I marked your question as most helpful not only because it's the only one, but I think it'll help others with similar issues. – Train Jul 17 '18 at 19:31
0

Id don´t know how to create, but to navigate use pushd C:\Path

ilidiocn
  • 323
  • 2
  • 5