Reputation: 45
I need to check for the available browsers in any Windows system by running a .bat file. The batch file should be able to check if Firefox and Chrome or any other browser is installed in system. Otherwise I want to open the default browser.
if exist "C:\Program Files(x86)\Mozilla Firefox\firefox.exe" start firefox.exe
This is a way to do this, but it won't work if the installation is done in some other location.
Upvotes: 2
Views: 5320
Reputation: 11367
I would recommend using the HKEY_LOCAL_MACHINE\SOFTWARE\Clients\StartMenuInternet
key. It lists the web browser clients that have registered themselves on the system and provides which one is the default and where to find each of them on the system.
See http://msdn.microsoft.com/en-us/library/windows/desktop/dd203067(v=vs.85).aspx for more details.
Here is just an example I threw together to illustrate how to use the information. Adjust as you need it or based upon your requirements.
@echo off
setlocal EnableDelayedExpansion
:Menu
echo 32-bit
set "Count=1"
for /f "tokens=2,*" %%A in ('reg query "HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Clients\StartMenuInternet" /ve 2^>nul') do set "Default=%%B"
for /f "skip=3 delims=" %%A in ('reg query "HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Clients\StartMenuInternet" 2^>nul') do (
if "%%~nxA"=="%Default%" (
echo !Count!. %%~nA [Default]
) else (
echo !Count!. %%~nA
)
set /a "Count+=1"
)
echo.
echo 64-bit if 32-bit above, else 32-bit
for /f "tokens=2,*" %%A in ('reg query "HKEY_LOCAL_MACHINE\SOFTWARE\Clients\StartMenuInternet" /ve 2^>nul') do set "Default=%%B"
for /f "skip=3 delims=" %%A in ('reg query "HKEY_LOCAL_MACHINE\SOFTWARE\Clients\StartMenuInternet" 2^>nul') do (
if "%%~nxA"=="%Default%" (
echo !Count!. %%~nA [Default]
) else (
echo !Count!. %%~nA
)
set /a "Count+=1"
)
echo.
:Input
set "Input="
set /p "Input=> Select a Browser: "
if not defined Input goto Input
set "Input=%Input:"=%"
set "Count=1"
:: NOTE if the browser name is typed out and matching on the name then the last match will be chosen.
:: As it is currently setup this means that the 32-bit version will always win on a name match.
for /f "skip=3 delims=" %%A in ('reg query "HKEY_LOCAL_MACHINE\SOFTWARE\Clients\StartMenuInternet" 2^>nul') do (
if /i "%Input%"=="%%~nA" set "Choice=%%~A"
if "%Input%"=="!Count!" set "Choice=%%~A"
set /a "Count+=1"
)
for /f "skip=3 delims=" %%A in ('reg query "HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Clients\StartMenuInternet" 2^>nul') do (
if /i "%Input%"=="%%~nA" set "Choice=%%~A"
if "%Input%"=="!Count!" set "Choice=%%~A"
set /a "Count+=1"
)
if not defined Choice goto Menu
for /f "tokens=2,*" %%A in ('reg query "%Choice%\shell\open\command" /ve 2^>nul') do set "Command=%%~B"
start "Browser" "%Command%"
endlocal
echo Done
pause>nul
Upvotes: 5
Reputation: 57272
You'll need eventually to start this as administrator because it exports values from the registry.I didn't use REG command for max compatibility:
@echo off
START /W REGEDIT /E "%Temp%.\BROW.reg" HKEY_CURRENT_USER\Software\Classes
setlocal enableDelayedExpansion
for /f "tokens=4 delims=\" %%B in ('type %Temp%.\BROW.reg ^| findstr /E "HTML\shell]"') do (
set "browser=%%B"
echo !browser:~0,-4!
)
endlocal
del /Q /F "%Temp%.\BROW.reg"
EDIT
@echo off
setlocal enableExtensions
echo.
echo.
echo INSTALLED BROWSERS
echo.
echo.
rem :::::::::::::::::::::::::::::::::::::::::::::::::::::
rem :: exporting registry values for installed browsers
rem :::::::::::::::::::::::::::::::::::::::::::::::::::::
rem for 64 bit systems
START /W REGEDIT /E "%Temp%\BROW3.reg" HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Clients\StartMenuInternet
rem for 32 bit systems
if not exist "%Temp%\BROW3.reg" START /W REGEDIT /E "%Temp%\BROW3.reg" HKEY_LOCAL_MACHINE\SOFTWARE\Clients\StartMenuInternet
setLocal enableDelayedExpansion
for /f "tokens=*" %%B in ('type "%Temp%\BROW3.reg" ^| findstr /E "DefaultIcon]"') do (
rem extracting browser name from icon path
set "browser=%%B"
rem removing \DefaultIcon] string
set "browser=!browser:\DefaultIcon]=!"
rem get the browser name
for %%P in ("!browser!") do echo %%~nP
)
endLocal
echo.
echo.
echo EXECUTABLES PATHS
echo.
echo.
setLocal enableDelayedExpansion
for /f "tokens=* delims=@=" %%B in ('type "%Temp%\BROW3.reg" ^| findstr /B "@" ^| findstr /E ".exe\\\",0\"^"') do (
set "browser=%%~B"
set "browser=!browser:\\=\!"
echo !browser!
)
setLocal enableDelayedExpansion
for /f "tokens=* delims=@=" %%B in ('type "%Temp%\BROW3.reg" ^| findstr /B "@" ^| findstr /E ".exe,0\"^"') do (
set "browser=%%~B"
set "browser=!browser:\\=\!"
set "browser=!browser:,0=!"
echo !browser!
)
endLocal
rem delete temp file
del /Q /F "%Temp%\BROW3.reg"
echo.
echo.
echo DEFAULT BROWSER
echo.
echo.
START /W REGEDIT /E "%Temp%\BROW5.reg" HKEY_CLASSES_ROOT\http\shell\open\command
setLocal enableDelayedExpansion
for /f tokens^=3^ delims^=^" %%B in ('type "%Temp%\BROW5.reg" ^| find "@"') do (
set "default=%%B"
rem removing double slashes
set "default=!default:\\=\!"
rem removing end slash
set "default=!default:~0,-1!"
rem get the name
for %%D in ("!default!") do echo %%~nD
)
endLocal
del /Q /F "%Temp%\BROW5.reg"
echo.
echo.
echo DEFAULT .HTML VIEWER
echo.
echo.
START /W REGEDIT /E "%Temp%\BROW6.reg" HKEY_CLASSES_ROOT\htmlfile\shell\open\command
setLocal enableDelayedExpansion
for /f tokens^=3^ delims^=^" %%B in ('type "%Temp%\BROW6.reg" ^| find "@"') do (
set "default=%%B"
set "default=!default:\\=\!"
set "default=!default:~0,-1!"
for %%D in ("!default!") do echo %%~nD
)
endLocal
del /Q /F "%Temp%\BROW6.reg"
echo.
echo.
pause
Upvotes: 0