I hope you can help me out with this issue, which I thought would be quite trivial but is turning out to be quite a bugger. As mentioned in the subject line this is what I want to accomplish:

  • Automatically run 5 command prompts, each new command prompt needs to do the following
  • Change directories to a specific location
  • start the built in php web server

This is the code I currently have for my .bat file:

start cmd /k
cd "directory 1"
php -S localhost:8081

start cmd /k
cd "directory 02"
php -S localhost:8082

start cmd /k
cd "directory 03"
php -S localhost:8083

etc...

Update (here are the working commands after some tweaking)

start "Dir1" /d "directory1" cmd /k "php -S localhost:8081"
start "Dir2" /d "directory2" cmd /k "php -S localhost:8082"
start "Dir3" /d "directory3" cmd /k "php -S localhost:8083"
start "Dir4" /d "directory4" cmd /k "php -S localhost:8084"
start "Dir5" /d "directory5" cmd /k "php -S localhost:8085"
start "Dir6" /d "directory6" cmd /k "php -S localhost:8086"

Thanks very much. Cheers, Tim K

1

There are 1 answers

10
Stephan On BEST ANSWER

The command has to be on the same line as cmd /k, else you start a new window and execute the next line in the original window. Start has a /d switch to set the working folder for the new process, so you don't need cd:

start "Dir1" /d "directory 1" cmd /k "php -S localhost:8081"
REM etc.

When the folder names and ports are accurate, you can use a for /L loop to simplify things:

for /l %%i in (1,1,5) do start "Dir %%i" /d "directory %%i" cmd /k "php -S localhost:808%%i"