2

I have a seemingly trivial question but can't figure it out. I am writing a batch script that opens Firefox with a specific URL (which works) and then does more stuff (which also works when executed in isolation). However, if I put both commands together in a batch script, they get executed back-to-back. I need the script to wait until Firefox is closed, though. Is that possible?

Here's what I have:

echo step one
start /wait "C:\Program Files (x86)\Mozilla Firefox\firefox.exe" http://www.google.com
echo step two

I want to only see "step two" after I close Firefox. I found this thread which seems very similar. But adding /wait doesn't seem to help. And I don't quite know how to adapt the accepted solution to my needs.

Any help would be greatly appreciated!

EDIT: This is how I adapted the scripted in the thread I linked to but it produces the same results:

echo step one
start /wait "C:\Program Files (x86)\Mozilla Firefox\firefox.exe" http://www.google.com

:LOOP
PSLIST firefox >nul 2>&1
IF ERRORLEVEL 1 (
  GOTO CONTINUE
) ELSE (
  ECHO Firefox is still running
  SLEEP 5
  GOTO LOOP
)

:CONTINUE
echo step two

But that still produces the same behavior. :s

Community
  • 1
  • 1
Florian
  • 579
  • 1
  • 10
  • 19
  • What part do you not know how to adapt? Replace `wordpad` with `firefox`. – Ken White Apr 22 '15 at 17:32
  • Hi Ken. That's what I tried but that didn't seem to do anything. (See my edit above.) Any ideas? – Florian Apr 22 '15 at 17:50
  • Not sure if this will work but try using `call` instead of `start`. I know that start will open a batch file in a new cmd instance and call will open it in the same instance. – Randy Rakestraw Apr 22 '15 at 18:22
  • Thanks - I just tried it but it doesn't work. Can't see a difference between `start` and `call`. :( – Florian Apr 22 '15 at 20:27

2 Answers2

1

Try this:

echo step one
"C:\Program Files (x86)\Mozilla Firefox\firefox.exe" http://www.google.com
:s1
for /f "tokens=*" %%a in (`tasklist /FI "IMAGENAME eq firefox.exe" /FI "STATUS eq running"`) do (if "%%~a" EQU "INFO: No tasks are running which match the specified criteria." goto s2)
goto s1
:s2
echo step two

Which should work

Monacraft
  • 6,510
  • 2
  • 17
  • 29
  • This seems to get me one step closer: the script is paused while Firefox is open. But I seem to be stuck in an endless loop when I close Firefox. It says, `The system cannot find the file tasklist`. – Florian Apr 23 '15 at 07:44
0

Oh my god! I just solved my own problem. It is as simple as this:

echo step one
"C:\Program Files (x86)\Mozilla Firefox\firefox.exe" http://www.google.com
echo step two

I'll go and cry in a corner now...

Florian
  • 579
  • 1
  • 10
  • 19
  • That's not how firefox or chrome work. When you run the executable it creates a process and then the executable exits, causin the batch file to continue. – Monacraft Apr 23 '15 at 09:17
  • Fool or not, it seems to work for me without a problem. – Florian Apr 24 '15 at 12:48