Batch - Default Browser?

前端 未结 8 1106
北荒
北荒 2021-02-12 16:09

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

相关标签:
8条回答
  • 2021-02-12 16:22

    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 /?
    
    0 讨论(0)
  • 2021-02-12 16:27

    Thank you, @Rob.

    @Rob's answer is close, but it still only pulls the ProgId. This one will set the browser name as reported by the ProgId (not always what you expect) in the browser variable, the specific verb used as browserverb, and the cmd path as browsercmd:

    setlocal enableDelayedExpansion
    :: parse the ProgId
    FOR /F "usebackq tokens=1,2,* delims==" %%A IN (`REG QUERY "HKCU\Software\Microsoft\Windows\Shell\Associations\UrlAssociations\http\UserChoice" /v ProgId 2^>NUL ^| more +2`) DO (
        SET browserstub=%%C
        :: parse for Class information
        FOR /F "usebackq tokens=1,2,* delims==" %%R IN (`REG QUERY "HKCR\!browserstub!" /v AppUserModelId 2^>NUL`) DO SET "browser=%%T"
        FOR /F "usebackq tokens=1,2,* delims==" %%R IN (`REG QUERY "HKCR\!browserstub!\shell" /ve 2^>NUL`) DO SET "browserverb=%%T"
        FOR /F "usebackq tokens=1,2,* delims==" %%R IN (`REG QUERY "HKCR\!browserstub!\shell\!browserverb!\command" /ve 2^>NUL`) DO SET "browsercmd=%%T"
    )
    :: display results
    SET browser
    
    0 讨论(0)
  • 2021-02-12 16:34

    Simply use

    start www.google.com

    See here

    0 讨论(0)
  • 2021-02-12 16:38

    Andy E's answer does not seem to work for me, it opens IE instead of Chrome.
    But, if I use http instead of htmlfile it does. Like this.

    reg QUERY HKEY_CLASSES_ROOT\http\shell\open\command /ve
    
    0 讨论(0)
  • 2021-02-12 16:39

    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.

    0 讨论(0)
  • 2021-02-12 16:45

    I hope this helps someone. I needed to start the default browser with a html file.

    @echo off
    setlocal
    
    rem setup a default browser in case we fail
    set default_browser=C:\Program Files\Internet Explorer\iexplore.exe
    
    rem look in the HKEY_CLASSES_ROOT\htmlfile\shell\open\command registry for the default browser
    for /f "tokens=*" %%a in ('REG QUERY HKEY_CLASSES_ROOT\htmlfile\shell\open\command /ve ^| FIND /i "default"') do (
       set input=%%a
    )
    setlocal enableDelayedExpansion
    
    rem parse the input field looking for the second token
    for /f tokens^=^2^ eol^=^"^ delims^=^" %%a in ("!input!") do set browser=%%a
    
    setlocal disableDelayedExpansion
    
    rem this may not be needed, check if reg returned a real file, if not unset browser
    if not "%browser%" == "" if not exist "%browser%" set browser=
    if "%browser%"=="" set browser=%default_browser%
    "%browser%" index.html
    endlocal
    
    0 讨论(0)
提交回复
热议问题