0

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

Tim Kruger
  • 863
  • 2
  • 10
  • 26

1 Answers1

2

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"
Stephan
  • 53,940
  • 10
  • 58
  • 91
  • Thanks very much Stephan however this only seems to start the 1st cmd prompt, and not the others? I'll update my original question with the commands I now have in the .bat file. – Tim Kruger Apr 02 '21 at 09:57
  • the first code opens just one prompt, the second one should start five of them (given, the folders `directory %%i` exist (like you question suggests) - if a folder doesn't exist, no `cmd` is executed and you get an errormessage `invalid directory` instead) – Stephan Apr 02 '21 at 10:05
  • Thanks once again Stephan, this does seem to be work after a bit of tweaking ;-). – Tim Kruger Apr 02 '21 at 10:06
  • Just out of curiosity: what did you tweak? – Stephan Apr 02 '21 at 10:06
  • Please take a look at my updated question for what I needed to tweak. – Tim Kruger Apr 02 '21 at 10:09
  • Hm. `start cmd /d "dir6" /k "php -S localhost:8086"` starts `cmd /d "dir6" /k ""`, which doesn't make much sense. Sure you don't want `start /d "dir6" cmd /k ""`? Then `for /l %%i in (1,1,6) do start "%%i" /d "dir%%i" cmd /k "php -S localhost:808%%i"` should work. – Stephan Apr 02 '21 at 10:21
  • Yeah it doesn't actually seem to be working correctly, however it was due to me misunderstanding you, I think I might have it working now though Lol. – Tim Kruger Apr 02 '21 at 10:26
  • Ok now it's working 100% as expected, please see my updated question. I'll take a look at the for structure you mentioned and see if I can get it working using that rather as it'll definitely be less code, however I'm just concerned about readability for new developers who aren't quite experienced ;-). – Tim Kruger Apr 02 '21 at 10:32
  • @TimothyKruger the foundation of every programming language has looping. That `FOR` command is pretty basic. All it is doing it substituting the the value of the iteration into your original code. It is pretty easy to see what `%%i` is replacing based on your hard coded example. – Squashman Apr 02 '21 at 13:59
  • @Squashman I totally agree with you that the `FOR` loop structure is very basic, however I'm new to scripting as a whole and my requirements are a bit more complex than what I initially shared, e.g. amongst other things ideally what I would like to do is give each cmd prompt a more descriptive title, i.e. "Google", "Hello Peter", etc. rather than "Dir1", "Dir2" etc. amongst other things which off the top of my head I'm assuming I'll need to create a 2 dimensional array 1st populating it with values and then use that to populate the `FOR` loop making it a bit more complicated ;-). – Tim Kruger Apr 02 '21 at 14:40