How to open an HTML file from a batch file in default browser with a specific destination anchor?

If you want to start the default browser then you should obviously not hardcode iexplore! The start command will figure out the system default on its own based on the file extension or URL protocol.

On my Windows 8.1 machine anchors work fine:

start "" "http://example.com#whatever"

it does seem to work without quotes as well but I personally prefer them just in case there is a space or a & there.

In my experience Internet Explorer will sometimes do some kind of internal redirection when dealing with local .htm[l] files and/or file:// URLs and this seems to strip away the anchor. There is not much you can do about that.

Just to be clear, appending a anchor to a plain file system path will not work because it technically changes the extension. A file:// URL will work because Windows only looks at the protocol when dealing with URLs.

On my system start "" "file:///C:/Program%20Files/Common%20Files/microsoft%20shared/Stationery/Soft%20Blue.htm#whatever" does work as far as accepting the command and starting the browser. This does not mean that the browser will respect the anchor but that is a implementation detail in the browser that you have no control over and results might vary depending on which browser is the default.

If this is not good enough then you can try using IE Automation to start and control Internet Explorer. This means you have to give up using the users default browser and I would not recommend it for this reason...

Relying on the file:// protocol and a .html extension to execute a browser and not a HTML editor is somewhat risky as pointed out in another answer but I'm not sure if I would dare to query the registry in a batch-file either.

The absolute best solution is to call ShellExecuteEx with a forced progid but that is only possible in a real application.

The second best solution is to write a Windows Scripting Host script instead of a batch file since it is much safer to read and parse the registry there...


In the case of opening files, using file:// is the same as calling the file directly.

That is,

start "" "file://%cd:\=/%/myfile.html"

is the same as

start "" "myfile.html"

Which is all well and good if your default program for opening HTML files is a browser, but it may not be.

To use the browser, the only thing I can think of is getting it from the registry:

@echo off
setlocal EnableDelayedExpansion
for /F "tokens=3" %%i in ('reg query HKEY_CURRENT_USER\Software\Microsoft\Windows\Shell\Associations\UrlAssociations\http\UserChoice /v ProgID') do for /F "tokens=2*" %%j in ('reg query HKEY_LOCAL_MACHINE\Software\Classes\%%i\shell\open\command /ve') do set cmd=%%k
start "" !cmd:%%1=file://%cd:\=/%/%1!

To explain, this retrieves the association with the "http" URL schema then looks up the execution command for that entry and stores it in cmd. We use %%k instead of j here because it will likely contain spaces, so 2 (%%j) selects the second entry in the return from reg then collected the rest into %%k.

Then it uses start to call the cmd with the target file (%1 here) is spliced into the %1 in the cmd. We assume the passed command is relative and make it absolute by prefixing it with the %cd% with the \ replaced with / as URLs need, then prefixed with the file schema for the browser.

I don't know how robust this is offhand, for instance the registry addresses my be different per system (I did this on Win10).


To open file from cmd Type: start "index.html"