barak manos
barak manos

Reputation: 30146

Weird behavior of a batch script for opening multiple CMD windows

I have the following directory structure:

I've created the StartAll.bat script in order to open a CMD window in each folder:

start cmd.exe /k cd Folder1 & set prompt=Folder1:
start cmd.exe /k cd Folder2 & set prompt=Folder2:
start cmd.exe /k cd Folder3 & set prompt=Folder3:
start cmd.exe /k cd Folder4 & set prompt=Folder4:

However, I'm experiencing a very weird behavior when running it:

enter image description here

As you can see, a proper window for the last folder is missing.

I'm observing the exact same behavior regardless of the number of folders.


Any idea what is going on here?

I would also appreciate alternative suggestions for achieving this purpose.

For all it matters, I am running this on Windows 10.

Thank you for your help.

Upvotes: 2

Views: 69

Answers (1)

Dennis van Gils
Dennis van Gils

Reputation: 3471

You should use this:

start cmd.exe /k cd Folder1 ^& set prompt=Folder1:
start cmd.exe /k cd Folder2 ^& set prompt=Folder2:
start cmd.exe /k cd Folder3 ^& set prompt=Folder3:
start cmd.exe /k cd Folder4 ^& set prompt=Folder4:

Right now the & character is not escaped which causes it to view the second part of your command as a new command within the scope of startAll.bat

This causes your commands to be executed like this:

  1. start a new window, which inherits the old prompt
  2. cd to a folder in the new window
  3. set the prompt of the old window to the folder name
  4. go back to 1, however the prompt of the old window changed

which then causes your 4 new windows to have prompts like this:

  1. the old, original prompt, which is most likely the path to startAll.bat
  2. folder 1 (note that this is the window that cded into folder2

etc...

Upvotes: 2

Related Questions