17

I am trying to open an HTML file with a specific destination anchor from a batch file like so:

start iexplore %~dps0nl752.htm#01
exit    

nl753.htm is on the local drive.

How can I get Windows to open the HTML file with the destination anchor in the default browser instead of Internet Explorer?

Mofi
  • 46,139
  • 17
  • 80
  • 143
David Mills
  • 181
  • 1
  • 1
  • 3
  • Why don't you pass the path of the default browser? – Yeikel Dec 31 '16 at 19:43
  • @Yeikel - How would he know what the default browser is set to? – SomethingDark Dec 31 '16 at 19:47
  • 2
    Also, you should be able to just say `start %~dps0nl752.htm#01` and that will open the file with the default browser automatically. – SomethingDark Dec 31 '16 at 19:48
  • @SomethingDark probably your suggestion is better – Yeikel Dec 31 '16 at 19:49
  • 2
    Well there you go. I've been trying to figure out the best way to parse the [req query for getting the default browser](http://stackoverflow.com/a/2178637/4158862) but it's different on Windows 10 vs Windows XP and I don't know what OS is being used. – SomethingDark Dec 31 '16 at 19:51
  • 2
    @David-Mills Off-topic info: [ID and NAME tokens](http://www.w3.org/TR/html401/types.html#type-name) must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), and periods (".") according to HTML 4.01 specification. Therefore `01` is an invalid anchor in HTML 4.01. But it is a valid anchor in HTML5 if defined in the HTML file with __id__ attribute and the HTML file is declared as HTML5 file. See also [The id attribute got more classy in HTML5](https://mathiasbynens.be/notes/html5-id-class) by Mathias Bynens. – Mofi Dec 31 '16 at 20:27

5 Answers5

16

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...

Anders
  • 97,548
  • 12
  • 110
  • 164
  • Did you try with a local file? Even if the title doesn't request it, his example with the local file seems to be unsolvable with default(unknown) browser. [edge](http://stackoverflow.com/questions/12021781/the-right-way-of-setting-a-href-when-its-a-local-file) Handing a file-URL to firefox or chrome with an anchor fails also. Using start.exe or explorer.exe to choose the browser fail also not recognizing the extension with appended #anchor. –  Dec 31 '16 at 21:42
  • @LotPings Using a proper file:// URL should work as far as the batch file and the Windows shell are concerned but what different browsers do when given such a URL is out of your control. – Anders Dec 31 '16 at 22:23
10

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).

Sweet Sheep
  • 326
  • 1
  • 6
  • The UserChoice key is relatively new and I'm not sure if it exists on Windows 7. The open verb is not necessarily the correct verb under the shell key but for the http progid it will be for 99.9% of machines. Doing this registry lookup correctly in a batch file is basically impossible but it should be good enough for most people if you just hardcode http as the progid if UserChoice is not there... – Anders Jan 01 '17 at 08:03
3

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

1
  ShVerb

  Lists or runs an explorer verb (right click menu) on a file or folder

    ShVerb <filename> [verb]

  Used without a verb it lists the verbs available for the file or folder

  The program lists most verbs but only ones above the first separator
  of the menu work when used this way

  The Properties verb can be used. However the program has to keep running
  to hold the properties dialog open. It keeps running by displaying
  a message box.

The VBS script is

HelpMsg = vbcrlf & "  ShVerb" & vbcrlf & vbcrlf & "  David Candy 2014" & vbcrlf & vbcrlf & "  Lists or runs an explorer verb (right click menu) on a file or folder" & vbcrlf  & vbcrlf & "    ShVerb <filename> [verb]" & vbcrlf & vbcrlf & "  Used without a verb it lists the verbs available for the file or folder" & vbcrlf & vbcrlf
HelpMsg = HelpMsg & "  The program lists most verbs but only ones above the first separator" & vbcrlf & "  of the menu work when used this way" & vbcrlf & vbcrlf 
HelpMsg = HelpMsg & "  The Properties verb can be used. However the program has to keep running" & vbcrlf & "  to hold the properties dialog open. It keeps running by displaying" & vbcrlf & "  a message box." 
Set objShell = CreateObject("Shell.Application")
Set Ag = WScript.Arguments 
set WshShell = WScript.CreateObject("WScript.Shell") 
Set fso = CreateObject("Scripting.FileSystemObject")

    If Ag.count = 0 then 
        wscript.echo "  ShVerb - No file specified"
        wscript.echo HelpMsg 
        wscript.quit
    Else If Ag.count = 1 then 
        If LCase(Replace(Ag(0),"-", "/")) = "/h" or Replace(Ag(0),"-", "/") = "/?" then 
            wscript.echo HelpMsg 
            wscript.quit
        End If
    ElseIf Ag.count > 2 then 
        wscript.echo vbcrlf & "  ShVerb - To many parameters" & vbcrlf & "  Use quotes around filenames and verbs containing spaces"  & vbcrlf
        wscript.echo HelpMsg 
        wscript.quit
    End If

    If fso.DriveExists(Ag(0)) = True then
        Set objFolder = objShell.Namespace(fso.GetFileName(Ag(0)))
'       Set objFolderItem = objFolder.ParseName(fso.GetFileName(Ag(0)))
        Set objFolderItem = objFolder.self
        msgbox ag(0)
    ElseIf fso.FolderExists(Ag(0)) = True then
        Set objFolder = objShell.Namespace(fso.GetParentFolderName(Ag(0)))
        Set objFolderItem = objFolder.ParseName(fso.GetFileName(Ag(0)))
    ElseIf fso.fileExists(Ag(0)) = True then
        Set objFolder = objShell.Namespace(fso.GetParentFolderName(Ag(0)))
        Set objFolderItem = objFolder.ParseName(fso.GetFileName(Ag(0)))
    Else
        wscript.echo "  ShVerb - " & Ag(0) & " not found"
        wscript.echo HelpMsg 
        wscript.quit
    End If

    Set objVerbs = objFolderItem.Verbs

    'If only one argument list verbs for that item

    If Ag.count = 1 then
        For Each cmd in objFolderItem.Verbs
            If len(cmd) <> 0 then CmdList = CmdList & vbcrlf & replace(cmd.name, "&", "") 
        Next
        wscript.echo mid(CmdList, 2)

    'If two arguments do verbs for that item

    ElseIf Ag.count = 2 then
        For Each cmd in objFolderItem.Verbs
            If lcase(replace(cmd, "&", "")) = LCase(Ag(1)) then 
                wscript.echo(Cmd.doit)
                Exit For
            End If
        Next
    'Properties is special cased. Script has to stay running for Properties dialog to show.
        If Lcase(Ag(1)) = "properties" then
            WSHShell.AppActivate(ObjFolderItem.Name & " Properties")
            msgbox "This message box has to stay open to keep the " & ObjFolderItem.Name & " Properties dialog open."
        End If  
    End If
End If

It gives output like this when listing available verbs

C:\Windows\system32>cscript //nologo "C:\Users\User\Desktop\ShVerb.vbs" "C:\Users\User\Desktop\Filter.html"

Open
Open in Same Window
Print
Restore previous versions
Cut
Copy
Create shortcut
Delete
Rename
Properties

To use to open an html page.

cscript "C:\Users\User\Desktop\Bat&Vbs\ShVerb.vbs" "C:\Users\User\Desktop\Filter.html" Open
0

To start any file in the system defaults you need to use the 'start' command on Windows 10 (Not sure about lower versions but I'm guessing it will be about the same) & 'open' for MacOS Terminals.

On Windows,

  • To open any file use: *start 'filename.format'

This should open up the files in default applications, like HTML files in browsers, images in albums, etc.