tags:

views:

33

answers:

3

Is their a way for a batch file to find the default browser on my computer?

A: 

If you're looking for a Windows .bat solution, this should work on Windows 2000 and later:

reg QUERY HKEY_CLASSES_ROOT\htmlfile\shell\open\command /ve

Result (on my Windows machine)

HKEY_CLASSES_ROOT\htmlfile\shell\open\command
(Default)    REG_SZ    "C:\Program Files (x86)\Internet Explorer\iexplore.exe" -nohome

See the REG.EXE help for more information:

REG /?
Andy E
This is not 100% correct, it will work in most cases (You probably want to check HTTP since .html could point to something other than htmlfile, "open" might not be the default verb etc)
Anders
@Anders: Sounds true enough, and in fact http was the first one I checked. However, when I checked HTTP on my Windows 7 machine it pointed to Firefox whilst IE was my default browser. Launching a url (http://google.com) opened IE, not Firefox. FWIW, I liked your answer :-) +1.
Andy E
A: 

This code will set the Environment variable browser to FirefoxURL which gives you a good indicator:

@ECHO OFF
REG QUERY HKCU\Software\Microsoft\Windows\Shell\Associations\UrlAssociations\http\UserChoice > browser.txt
FOR /F "skip=2 tokens=*" %%G IN (browser.txt) DO ( SET browser=%%G )
SET browser=%browser:~20%
ECHO Is: %browser%

It works by querying the registry to a text file then skipping the first two lines (useless for your purposes), then taking the text that starts at the 20th character of te remaining line. Which gives you FirefoxURL

Rob
+2  A: 

It is impossible to do this 100% correct in a batch file since the default command could come from a COM object and not a string in the registry (MayChangeDefaultMenu will force IContextMenu to be called for double-clicks and could change the default action)

Here is some code that tries to do the right thing (The fallback verb is open, it really should be the first subkey, but I did not feel like dealing with that)

@echo off
setlocal ENABLEEXTENSIONS
set progid=htmlfile&set verb=open&set browsercmd=
FOR /F "skip=2 tokens=2 delims=_" %%a IN ('2^>nul REG QUERY "HKCR\.html" /ve^|find /V ""^|find /V "HKEY_"') DO FOR /F "tokens=1,*" %%b IN ("%%~a") DO if not "%%~c"=="" set progid=%%~c
FOR /F "skip=2 tokens=2 delims=_" %%a IN ('2^>nul REG QUERY "HKCR\%progid%\shell" /ve^|find /V ""^|find /V "HKEY_"') DO FOR /F "tokens=1,*" %%b IN ("%%~a") DO if not "%%~c"=="" set verb=%%~c
FOR /F "skip=2 tokens=2 delims=_" %%a IN ('2^>nul REG QUERY "HKCR\%progid%\shell\%verb%\command" /ve^|find /V ""^|find /V "HKEY_"') DO FOR /F "tokens=1,*" %%b IN ("%%~a") DO if not "%%~c"=="" set browsercmd=%%c

echo.DefaultBrowser=%browsercmd%

This code probably has problems, but at least it tries to find the correct verb. You also have to deal with the fact that the returned string could contain "%1".

If all you really want to do is open a URL, all you need is start http://example.com If you want to open the browser, but not a specific URL, a ugly hack like start "" http://about:blank might just work.

Anders