0

I want to write a batch file to keep checking every 20 seconds if firefox is open, if open then there is no action required. if firefox is closed, the batch file needs to force open it on a certain webpage. this also needs to be a silent batch..

I've tried this so far:

@echo off
Set WShShell = WScript.CreateObject("WScript.Shell")
tasklist /fi "firefox.exe" 2>NUL | find /i /n "firefox.exe">NUL
if %ERRORLEVEL%==1 goto nofirefox
cmd /C start firefox.exe -new-tab "google.com" 
  • [What have you tried](http://mattgemmell.com/2008/12/08/what-have-you-tried/) so far? – rojo Mar 13 '13 at 13:15
  • echo off Set WShShell = WScript.CreateObject("WScript.Shell") tasklist /fi "firefox.exe" 2>NUL | find /i /n "firefox.exe">NUL / if %ERRORLEVEL%==1 goto nofirefox cmd /C start firefox.exe -new-tab "google.com" – user2165550 Mar 13 '13 at 13:19

2 Answers2

0

Your code is lazy (and the VBScript line is blowing my mind), but I see where you're going with it. The tasklist command is missing a bit. tasklist /fi "imagename eq firefox.exe" will search the task list for firefox. You need a loop tag with a ping to nowhere for the 20 second delay.

Try this.

@echo off
setlocal

:loop
wmic process where name="firefox.exe" get name /format:list 2>NUL | find /i "firefox" >NUL || call :relaunch
ping -n 21 0.0.0.0>NUL
goto loop

:relaunch
rem echo %time% - starting Firefox
start "" firefox.exe -new-tab http://www.google.com/
rojo
  • 24,000
  • 5
  • 55
  • 101
  • Thank you, but the command window is remaining open and is it possible to hide when the script runs? – user2165550 Mar 13 '13 at 13:47
  • I've never tried it, but you could experiment with running your batch script [as a service](http://stackoverflow.com/questions/415409/run-batch-file-as-a-windows-service). What you're asking about might've been better suited as an AutoIt script rather than a Windows batch script though. – rojo Mar 13 '13 at 13:51
0
@ECHO OFF
SETLOCAL
:loop
TASKLIST /fi "imagename eq firefox.exe" |FIND /i "firefox.exe" >nul
IF ERRORLEVEL 1 START firefox.exe -new-tab "google.com"
choice /t 20 /d y /n >NUL
IF ERRORLEVEL 2 GOTO :EOF 
GOTO loop

Pressing N (or ^C) will exit the loop.

(in XP, replace CHOICE... and the ERRORLEVEL 2 line with

PING -n 21 127.0.0.1 >nul  
if exists stopfile. del stopfile.&goto :eof

then create stopfile. to terminate this batch)

Magoo
  • 77,302
  • 8
  • 62
  • 84