Batch - Default Browser?

前端 未结 8 1126
北荒
北荒 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:47

    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

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

    bubble's answer has some caveats.

    This is what works in much more cases for me:

    start "" explorer "protocol://your.complicated/url?foo=bar"
    

    The only thing you have to escape is a double quote, and you escape it by typing it once more.

    Works at least for protocols http://, https:// and file:// (without query string). Doesn't work for ftp:// URLs (it opens them as a network drive).


    Fail cases of the accepted answer

    First, the webpage has to start with www.

    rem Works
    start www.google.com
    
    rem FAILS!
    start google.com
    The system cannot find the file google.com
    
    rem FAILS!
    start translate.google.com
    The system cannot find the file translate.google.com
    

    This can be fixed by prepending http://

    rem Works
    start http://google.com
    
    rem Works even for file:// protocol
    start file://C:/test/main.html
    
    rem Works - you can even pass QSA
    start http://google.com?foo=bar
    
    rem FAILS! outch, you have to escape ampersands
    start http://google.com?foo=bar&baz=baz
    'baz' is not recognized as an internal or external command, operable program or batch file.
    
    rem FAILS! but QSA only works for http/s URLs; for file:// protocol, it is ignored
    start file://D:/Programování/lumix-link/Control.html?foo=bar
    rem opens file://D:/Programování/lumix-link/Control.html
    

    One might e.g. think of enclosing the webpage in double quotes (to avoid misinterpretation of some characters). Then it fails and instead tries to open another CMD.exe with the given string as name:

    rem FAILS!
    start "http://google.com"
    

    Okay, this is not a bug, just a mislead. This is the expected behavior. Let's fix it:

    rem Works
    start "" "http://google.com"
    
    rem FAILS!
    start "" "google.com"
    The system cannot find the file google.com.
    

    So we really have to provide an executable that should be launched. You can see the super-long answers here that succeed better or worse in getting the path to the default browser. But what's much simpler and bullet-proof is to use explorer to launch the webpage. But since explorer decides on what app to launch based on the protocol, you have to use the protocol prefix. And that's it!

    rem FAILS!
    rem start "" explorer "google.com"
    
    rem Works
    rem start "" explorer "http://google.com"
    
    rem Works
    rem start "" explorer "http://google.com?foo=bar"
    
    rem FAILS! QSA still not supported on file:// URLs
    rem start "" explorer "file://C:/test/main.html?foo=bar"
    
    0 讨论(0)
提交回复
热议问题