Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

use cmd.exe to change directory and run command in that directory

Tags:

cmd

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.

like image 639
Stagg Avatar asked May 11 '12 13:05

Stagg


People also ask

Which CMD EXE command is used to switch to another directory location?

Cd is the abbreviation or synonym for chdir. It is a command found inside the Windows Command Processor (cmd) that allows for change of the current working directory of a shell instance.

How do I change my path in CMD?

If the folder you want to open in Command Prompt is on your desktop or already open in File Explorer, you can quickly change to that directory. Type cd followed by a space, drag and drop the folder into the window, and then press Enter. The directory you switched to will be reflected in the command line.

How do I run a command in a specific folder?

If you want run command window from a specific folder. Click your folder then hold your shift key, then right click you will found option open command window here then click it.


2 Answers

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" 
like image 196
Christian.K Avatar answered Oct 24 '22 00:10

Christian.K


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 
like image 32
Gratzy Avatar answered Oct 24 '22 00:10

Gratzy