如何使用特定目标锚点在默认浏览器中从批处理文件打开HTML文件?

时间:2016-12-31 19:30:15

标签: batch-file

我正在尝试从批处理文件中打开一个包含特定destination anchor的HTML文件,如下所示:

start iexplore %~dps0nl752.htm#01
exit    

nl753.htm位于本地驱动器上。

如何让Windows在默认浏览器而不是Internet Explorer中使用目标锚点打开HTML文件?

4 个答案:

答案 0 :(得分:7)

如果您想启动默认浏览器,那么您显然不应该硬编码iexplore! start命令将根据文件扩展名或URL协议自行确定系统默认值。

在我的Windows 8.1机器上,锚点工作正常:

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

它似乎也没有引号,但我个人更喜欢它们,以防有空格或&

根据我的经验,Internet Explorer在处理本地.htm [l]文件和/或file:// URL时有时会进行某种内部重定向,这似乎剥离了锚点。你无能为力。

为了清楚起见,将锚附加到普通文件系统路径将不起作用,因为它在技术上改变了扩展名。 file:// URL将起作用,因为Windows仅在处理URL时查看协议。

在我的系统上start "" "file:///C:/Program%20Files/Common%20Files/microsoft%20shared/Stationery/Soft%20Blue.htm#whatever"确实可以接受命令并启动浏览器。这并不意味着浏览器会尊重锚点,但这是您无法控制的浏览器中的实现细节,结果可能会因默认的浏览器而异。

如果这还不够好,那么您可以尝试使用IE Automation来启动和控制Internet Explorer。这意味着您必须放弃使用用户默认浏览器,因此我不推荐它...

依赖于file://协议和.html扩展来执行浏览器而不是HTML编辑器有点冒险,如另一个答案所指出的那样但是我不确定我是否敢于查询注册表在批处理文件中。

绝对最佳的解决方案是使用强制progid调用ShellExecuteEx,但这只能在实际应用程序中使用。

第二个最佳解决方案是编写Windows Scripting Host脚本而不是批处理文件,因为在那里读取和解析注册表更安全...

答案 1 :(得分:2)

在打开文件的情况下,使用file://与直接调用文件相同。

即,

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

相同
start "" "myfile.html"

如果您打开HTML文件的默认程序是浏览器,那么这一切都很好,但可能不是。

要使用浏览器,我唯一能想到的就是从注册表中获取它:

@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!

为了解释,这将检索与" http"的关联。然后,URL模式查找该条目的执行命令并将其存储在cmd中。我们在这里使用%% k而不是j,因为它可能包含空格,因此2(%% j)选择reg中返回的第二个条目,然后将其余条目收集到%% k中。

然后使用start来调用cmd,并将目标文件(此处为%1)拼接到cmd中的%1。我们假设传递的命令是相对的,并通过在%cd%前面加上\替换为/作为URL需要,然后以浏览器的文件模式为前缀,使其成为绝对命令。

我不知道这是多么强大,例如注册表解决了每个系统的不同(我在Win10上做了这个)。

答案 2 :(得分:0)

  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.

VBS脚本是

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

在列出可用动词

时给出这样的输出
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

用于打开html页面。

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

答案 3 :(得分:0)

要从cmd打开文件,请输入以下类型: 开始“ index.html”