Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for getdefaultbrowser.bat

(view source code of getdefaultbrowser.bat as plain text)

  1. @ECHO OFF
  2. :: No command line arguments accepted except /Q
  3. IF NOT "%~1"=="" IF /I NOT "%~1"=="/Q" GOTO Syntax
  4.  
  5. SETLOCAL ENABLEDELAYEDEXPANSION
  6. SET DefaultBrowser=
  7. SET QuietMode=0
  8. IF /I "%~1"=="/Q" SET QuietMode=1
  9.  
  10. FOR /F "tokens=2*" %%A IN ('REG.EXE Query "HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.html\UserChoice" /V ProgID') DO (
  11. 	FOR /F "tokens=2*" %%C IN ('REG.EXE Query "HKEY_CLASSES_ROOT\%%~B\shell\open\command"') DO (
  12. 		SET Executable=
  13. 		FOR %%E IN (%%D) DO IF "!Executable!"=="" SET Executable="%%~E"
  14. 	)
  15. 	FOR /F "delims=-" %%C IN ("%%~B") DO (
  16. 		SET DefaultBrowser=%%~C
  17. 		SET DefaultBrowser=!DefaultBrowser:HTML=!
  18. 		SET DefaultBrowser=!DefaultBrowser:HTM=!
  19. 		SET DefaultBrowser=!DefaultBrowser:URL=!
  20. 	)
  21. 	IF %QuietMode% EQU 0 (
  22. 		ECHO.
  23. 		ECHO For local .html files:
  24. 		ECHO Default browser = !DefaultBrowser!
  25. 		ECHO Program Path    = !Executable!
  26. 	)
  27. )
  28. FOR /F "tokens=2*" %%A IN ('REG.EXE Query "HKEY_CURRENT_USER\Software\Microsoft\Windows\Shell\Associations\UrlAssociations\http\UserChoice" /V ProgID') DO (
  29. 	FOR /F "tokens=2*" %%C IN ('REG.EXE Query "HKEY_CLASSES_ROOT\%%~B\shell\open\command"') DO (
  30. 		SET Executable=
  31. 		FOR %%E IN (%%D) DO IF "!Executable!"=="" SET Executable="%%~E"
  32. 	)
  33. 	FOR /F "delims=-" %%C IN ("%%~B") DO (
  34. 		SET DefaultBrowser=%%~C
  35. 		SET DefaultBrowser=!DefaultBrowser:HTML=!
  36. 		SET DefaultBrowser=!DefaultBrowser:HTM=!
  37. 		SET DefaultBrowser=!DefaultBrowser:URL=!
  38. 	)
  39. 	IF %QuietMode% EQU 0 (
  40. 		ECHO.
  41. 		ECHO For http and https URLs:
  42. 		ECHO Default browser = !DefaultBrowser!
  43. 		ECHO Program Path    = !Executable!
  44. 	)
  45. )
  46. ENDLOCAL & SET DefaultBrowser=%DefaultBrowser%
  47. EXIT /B 0
  48.  
  49.  
  50. :Syntax
  51. ECHO.
  52. ECHO GetDefaultBrowser.bat,  Version 1.03 for Windows 10/11
  53. ECHO Demo script to try and detect the default browser
  54. ECHO.
  55. ECHO Usage:    GetDefaultBrowser.bat  [ /Q ]
  56. ECHO.
  57. ECHO Where:    /Q forces Quiet mode, i.e. no screen output, only environment variable
  58. ECHO.
  59. ECHO Note:     This batch file searches the registry for the user's default browser
  60. ECHO           choice, first for local .html files, next for http(s) URLs.
  61. ECHO           The latter value will be stored in an environment variable named
  62. ECHO           DefaultBrowser.
  63. ECHO           The default browser's name and path are displayed unless /Q is used.
  64. ECHO           Return code ("ErrorLevel") 1 on command line errors, otherwise 0.
  65. ECHO.
  66. ECHO Credits:  Joe Caverly (.html) and Steve Robertson (URLs).
  67. ECHO.
  68. ECHO Written by Rob van der Woude
  69. ECHO https://www.robvanderwoude.com
  70.  
  71. EXIT /B 1
  72.  

page last modified: 2024-04-16; loaded in 0.0058 seconds