use cmd.exe to change directory and run command in

2019-03-24 05:45发布

问题:

All I want to do is:

  1. change to specific directory of a different drive
  2. run a command in that directory e.g. dir

I need to do this in one line using cmd.exe starting from a different drive

I would do this like this:

c:
cd temp
dir 

so in one statement so far I have:

cmd /c c: & cd\temp & dir

But this just gives me dir for the P: directory which I start from. How can I get dir returned from c:\temp?

I can't run a batch file and it must be in a one-line statement.

回答1:

You may want to invoke CD with the /d option, thus not only changing the current directory on drive c: but also going there (in case you are not already on that drive).

 cmd /c "cd /d c:\temp && dir"


回答2:

you use && or & to separate multiple commands

if the cmd window is already opened and running from command line

 c: && cd\temp && dir

or

cmd /c && c: && cd\temp && dir


回答3:

You want quotes around that command line:

cmd /c "cd c:/ & dir"



标签: cmd