Using GOTO
Try the following batch:
@echo off
set loopCount=50
:loop
start "C:\Program Files\Mozilla Firefox\firefox.exe" "http://testmywebsiteloadspeed.com"
set /a loopCount=%loopCount%-1
if %loopCount%==0 GOTO:EOF
timeout /t 60
taskkill /im firefox.exe
timeout /t 5
GOTO :loop
:loop
defines a GOTO
point.
timeout /t 60
waits for 60 seconds
taskkill /im firefox.exe
will kill the Firefox process
GOTO :loop
will reroute back to the beginning of the script.
Note: This will kill all running Firefox processes. I think Firefox generally only allows one at a time anyway.
Finite loop using GOTO
In order to do this for a specific number of iterations, you can use set /a
to do some math on a loopCount variable.
@echo off
set loopCount=50
:loop
start "C:\Program Files\Mozilla Firefox\firefox.exe" "http://testmywebsiteloadspeed.com"
set /a loopCount=%loopCount%-1
if %loopCount%==0 GOTO:EOF
timeout /t 60
taskkill /im firefox.exe
timeout /t 5
GOTO :loop
The set /a loopCount=%loopCount%-1
line will reduce the value of loopCount by one each time the loop runs. The if %loopCount%==0 GOTO:EOF
line will exit the script when it reaches 0.
GOTO:EOF
means "go to the end of the file", which will bypass any other code in the file.
Using FOR /l
An alternative solution would be to use a FOR
loop with the /l
switch, but I've had problems with this when using nested loops, so I generally stick to the GOTO
option of simple Batch files like this. Still, I'm hoping this will help someone in the future.
@echo off
for /l %%x in (1, 1, 50) do (
start "C:\Program Files\Mozilla Firefox\firefox.exe" "http://testmywebsiteloadspeed.com"
set /a loopCount=%loopCount%-1
if %loopCount%==0 GOTO:EOF
timeout /t 60
taskkill /im firefox.exe
timeout /t 5
)
To break down the FOR /l
command:
- The first argument is where you begin.
- The second argument is how big of a step to take. (This isn't relevant to your particular example since you don't use the value at all.)
- The third argument is when to stop.
start "" "..."
3rd usetaskkill /?
4th insert a:label
and agoto :label
at the end.