Batch file opens Default Browser instead of Firefox

9

3

I have a login script that runs for every user. The first check sees if the username matches our Test-Taking User (exam). If so, launches Firefox to the exam homepage and stops.

The commands individually work. When I call the .bat file, it launches Internet Explorer to the website. What am I doing wrong?

@echo off

REM Exam Startup - Username is "exam", then start the Exam website, and exit the script
if %USERNAME% EQU exam (
    if exist "%PROGRAMFILES%\Mozilla Firefox\firefox.exe"       start "%PROGRAMFILES%\Mozilla Firefox\firefox.exe" "https://www.example.com/"
    if exist "%PROGRAMFILES(x86)%\Mozilla Firefox\firefox.exe"  start "%PROGRAMFILES(x86)%\Mozilla Firefox\firefox.exe" "https://www.example.com/"
    exit
)
...
REM rest of script

Canadian Luke

Posted 2016-04-11T18:33:24.900

Reputation: 22 162

Answers

25

What am I doing wrong?

if exist "%PROGRAMFILES%\Mozilla Firefox\firefox.exe" start "%PROGRAMFILES%\Mozilla Firefox\firefox.exe" "https://www.example.com/"

You have no "title" in your start command.

  • If there is no "title" then start parses "%PROGRAMFILES%\Mozilla Firefox\firefox.exe" as a title (because it starts with a ") and "https://www.example.com/" as the command to execute.

  • Executing the command "https://www.example.com/" causes the default browser to open that URL.

Try adding "" after start:

if exist "%PROGRAMFILES%\Mozilla Firefox\firefox.exe" start "" "%PROGRAMFILES%\Mozilla Firefox\firefox.exe" "https://www.example.com/"

Syntax

START "title" [/D path] [options] "command" [parameters] Key:
  • title Text for the CMD window title bar (required.)
  • path Starting directory.
  • command The command, batch file or executable program to run.
  • parameters The parameters passed to the command.

...

Always include a title this can be a simple string like "My Script" or just a pair of empty quotes ""

According to the Microsoft documentation, the title is optional, but depending on the other options chosen you can have problems if it is omitted.

Source start


Further Reading

DavidPostill

Posted 2016-04-11T18:33:24.900

Reputation: 118 938

So if I understand this correctly, start interprets ...firefox.exe as the title and the URL as what should be started and is intelligent enough to detect an URL and feed it to the default browser? – Boldewyn – 2016-04-12T08:46:47.950

2@Boldewyn: start indeed knows what to do with URLs, and that's to open them in the default browser. – MSalters – 2016-04-12T08:48:20.153