1

I am trying to run a sequence of commands using the cmd command, the /k switch, and && to separate each command in the sequence.

For example, I would like to run this command to change to one folder, and then to the next:

cmd /k "cd C:\Program Files\Microsoft Office" && "cd C:\Program Files\Windows Mail"

But this doesn't work - it changes the folder only to Microsoft Office, and then stops.

How do I get this done?

Edit:

Actually, @seƱor-csmasmas is correct - my earlier example doesn't really demonstrate the problem.

The problem actually shows up when I run a command that has spaces in the path, followed by a command to change a directory.

For example, if you create a bat file which copies a file (let's call it copyfile.bat), and place the bat file in a folder with spaces, then call the cmd command like so:

C:\windows\system32\cmd.exe /k "C:\Test Folder\copyfile.bat" && "cd C:\Some Other Folder"

You will see that the current directory is not changed to C:\Some Other Folder.

2 Answers2

2

// Edit

The problem actually shows up when I run a command that has spaces in the path, followed by a command to change a directory.

For example, if you create a bat file which copies a file (let's call it copyfile.bat), and place the bat file in a folder with spaces, then call the cmd command like so:

C:\windows\system32\cmd.exe /k "C:\Test Folder\copyfile.bat" && "cd > C:\Some Other Folder"

:: try :: C:\windows\system32\cmd.exe /k "call "C:\Test Folder\copyfile.bat" && cd /d "C:\Some Other Folder""

Edit //


cmd /k "cd "C:\Program Files\Microsoft Office" && cd "C:\Program Files\Windows Mail""

Try to visualize how your commands and the operator will look likes between double quotes where necessary in a single cmd /k "command "with" operator and another command "with""

cmd /k "
   cd /d "C:\Program Files\Microsoft Office" 
   && 
   cd /d "C:\Program Files\Windows Mail"

   "

cmd /k " command "path #1" && command "path #2" "

Io-oI
  • 9,237
0

Before windows was DOS and before that was CP/M.

CP/M would generally only run single commands.

DOS increasingly had more and more abilities to split CPU usage for separate processes, thus you could run a secondary command.com from the primary level with its own environment.

So Windows inherited a need for several ways to call command processes, and thus 3 master switches still survive.

command.com should be dead
enter image description here

The new kid on the block is cmd.exe at it defaults to same behaviour as /K (keep active) and you can exit back to current cmd level here is your problem

enter image description here

However we often want to let a cmd run and return console control & continue so that is /c

So what is the third ? It is not listed !

enter image description here

enter image description here

so /c and /r are synonymous both act as run and continue

OK that was a diversion and does not answer the question which is potentially similar to this problem that the commands in a line are run before used (non delayed execution)

enter image description here

We could expect %CD% to report c:\users but clearly was not

So to your answer

You want to move to another location and start a fresh Konsole then you need to change the order and method

you have

cmd /k "cd C:\Program Files\Microsoft Office" && "cd C:\Program Files\Windows Mail"

So this works (may well be overkill ?)

enter image description here

cmd /c start " " cmd /r "cd /d "c:\program files" && cmd /k" && cd /d "c:\users"

we start a secondary process that will /run cd and /k then we change to another cwd and it should work the other way round too

So try this answer

start "Office Dir" cmd /k "cd C:\Program Files\Microsoft Office" && cd "C:\Program Files\Windows Mail"

optionally add a && dir to verify during test

If what you are after is two seperate consoles like this enter image description here

then use this order

cd /d "C:\Program Files\Microsoft Office" && start "office" cmd /k && cd /d "C:\Program Files\Windows Mail" && start "Mail" cmd /k
K J
  • 1,248