2

I am using Windows 10 Professional. I want to create a batch file:

  1. that opens the Firefox browser,
  2. navigates to a page
  3. closes the browser after a minute.
  4. Repeat steps 1 to 3.

I figured out the following code so far to write in my batch file:

@echo off
start "C:\Program Files\Mozilla Firefox\firefox.exe" "http://testmywebsiteloadspeed.com"
ping -n 1 -w 60 

But this isn't doing anything. Also how to I achieve 3 & 4?

6
  • Is using PowerShell an option? It is difficult to close the browser window using only CMD. Jul 16, 2018 at 16:09
  • 2
    Start uses the first argument in double quotes as the window title - insert an empty pair start "" "..." 3rd use taskkill /? 4th insert a :label and a goto :label at the end.
    – LotPings
    Jul 16, 2018 at 16:10
  • Why not use a plugin that auto-reloads the page?
    – Attie
    Jul 16, 2018 at 16:38
  • @LotPings I tried as suggested by you and Worthwelle. But it worked only for 4 times. Then it says"firefox closes unexpectedly while starting. this might be caused by add-ons. You can resolve by starting in Safe Mode". I disabled all add-ons however the problem persists.
    – CuriousDev
    Jul 16, 2018 at 16:39
  • @Attie Which one?
    – CuriousDev
    Jul 16, 2018 at 16:39

1 Answer 1

5

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.
7
  • This did work but only for 4 times. Then it says"firefox closes unexpectedly while starting. this might be caused by add-ons. You can resolve by starting in Safe Mode". I disabled all add-ons however the problem persists.
    – CuriousDev
    Jul 16, 2018 at 16:38
  • That's one of the risks of forcing programs to close through batch. They're unable to close the way they're designed. I've been trying to find a decent way around that for years.
    – Worthwelle
    Jul 16, 2018 at 16:41
  • 1
    You could try taking out the /f switch on taskkill and add a second timeout. This might allow Firefox to close more naturally. There's some more information here. I've updated the answer accordingly.
    – Worthwelle
    Jul 16, 2018 at 16:46
  • 1
    @CuriousDev You could also put some fail safe logic in there after taskkill of tasklist | findStr /i firefox.exe and then if %errorlevel% neq 0 (taskkill /im firefox.exe) or something of that nature should you see problems arise in the future. I'd probably have it go to another label and keep checking that firefox.exe is killed from memory otherwise keep killing it before starting another instance. Jul 16, 2018 at 16:59
  • 1
    One last question @Worthwelle . What if I have to run this loop a finite number of times only. Say 50 times. How would I do it? I believe it will be a For In loop? Can you please show it with a new example.
    – CuriousDev
    Jul 16, 2018 at 17:39

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .