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

Batch file 如何在默认浏览器中使用特定的目标定位从批处理文件打开HTML文件?,batch-file,Batch File,我试图从批处理文件中打开一个具有特定属性的HTML文件,如下所示: start iexplore %~dps0nl752.htm#01 exit nl753.htm位于本地驱动器上 如何让Windows在默认浏览器而不是Internet Explorer中使用目标定位打开HTML文件?如果要启动默认浏览器,则显然不应硬编码iexplore!start命令将根据文件扩展名或URL协议自行确定系统默认值 在我的Windows 8.1机器上,锚可以正常工作: start "" "http:/

我试图从批处理文件中打开一个具有特定属性的HTML文件,如下所示:

start iexplore %~dps0nl752.htm#01
exit    
nl753.htm
位于本地驱动器上


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

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

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

start "" "http://example.com#whatever"
它似乎在没有引号的情况下也能工作,但我个人更喜欢它们,以防有空格或
&

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

需要明确的是,在普通文件系统路径上附加一个锚点将不起作用,因为它在技术上改变了扩展名。文件://URL将起作用,因为Windows在处理URL时只查看协议

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

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

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

绝对最好的解决方案是使用强制progid调用ShellExecuteEx,但这仅在实际应用程序中才可能实现


第二个最佳解决方案是编写Windows脚本主机脚本,而不是批处理文件,因为读取和解析注册表要安全得多…

在打开文件的情况下,使用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%和\替换为/as URL,然后再加上浏览器的文件架构前缀

我不知道这有多强大,例如,注册表地址可能会因系统而异(我是在Win10上这样做的)

用于打开html页面

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

从cmd类型打开文件:
启动“index.html”

你为什么不传递默认浏览器的路径?@Yeikel-他怎么知道默认浏览器设置为什么?此外,你应该能够只说
start%~dps0nl752.htm
,这将使用默认浏览器自动打开文件。好了。我一直在试图找出解析的最佳方法,但在Windows 10和Windows XP上它是不同的,我不知道使用的是什么操作系统。@David Mills离题信息:必须以字母([a-Za-z])开头,后面可以跟任意数量的字母、数字([0-9])、连字符(“-”)、下划线(“)、冒号(:”)和句点(“。”根据HTML4.01规范。因此
01
在HTML4.01中是无效的锚定。但是,如果在具有id属性的HTML文件中定义了它,并且该HTML文件被声明为HTML5文件,则它是HTML5中的有效锚定。另请参见Mathias Bynens。您是否尝试使用本地文件?即使标题没有要求,他的本地文件示例似乎无法用默认(未知)浏览器解决。将文件URL传递给firefox或chrome(带有锚点)也会失败。使用start.exe或explorer.exe选择浏览器时也会失败,无法识别附加了#anchor的扩展名。@lotping使用正确的文件://URL应该对批处理文件和Windows shell起作用,但是当给定这样的URL时,不同的浏览器会做什么超出您的控制范围。UserChoice键相对较新,我很抱歉不确定它是否存在于Windows 7上。在shell键下,open动词不一定是正确的动词,但对于http progid,它将适用于99.9%的机器。在批处理文件中正确地执行此注册表查找基本上是不可能的,但是对于大多数人来说,如果没有UserChoice,您只需将http硬编码为progid,这应该足够好了。。。
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
cscript "C:\Users\User\Desktop\Bat&Vbs\ShVerb.vbs" "C:\Users\User\Desktop\Filter.html" Open