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



我正试图从批处理文件中打开一个带有特定目标锚点的HTML文件,如下所示:

start iexplore %~dps0nl752.htm#01
exit    

nl753.htm在本地驱动器上。

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

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

在我的Windows8.1机器锚工作良好:

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

它似乎也可以在没有引号的情况下工作,但我个人更喜欢它们,以防有空格或&

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

需要明确的是,将锚点附加到普通文件系统路径是不起作用的,因为它在技术上会更改扩展名。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脚本,而不是批处理文件,因为在那里读取和解析注册表要安全得多。。。

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

也就是说,

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

与相同

start "" "myfile.html"

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

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

@echo off
setlocal EnableDelayedExpansion
for /F "tokens=3" %%i in ('reg query HKEY_CURRENT_USERSoftwareMicrosoftWindowsShellAssociationsUrlAssociationshttpUserChoice /v ProgID') do for /F "tokens=2*" %%j in ('reg query HKEY_LOCAL_MACHINESoftwareClasses%%ishellopencommand /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中。我们假设传递的命令是相对的,并根据URL的需要,在它前面加上%cd%和替换为/的\,使其成为绝对的,然后加上浏览器的文件架构。

我不知道这有多健壮,例如,每个系统的注册表地址都不同(我在Win10上做过)。

要从cmd打开文件,请键入:启动"index.html">

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:Windowssystem32>cscript //nologo "C:UsersUserDesktopShVerb.vbs" "C:UsersUserDesktopFilter.html"
Open
Open in Same Window
Print
Restore previous versions
Cut
Copy
Create shortcut
Delete
Rename
Properties

用于打开html页面。

cscript "C:UsersUserDesktopBat&VbsShVerb.vbs" "C:UsersUserDesktopFilter.html" Open

要在系统默认值中启动任何文件,您需要在Windows 10上使用"start"命令(不确定较低版本,但我猜它会大致相同)&'为MacOS终端打开。

在Windows上,

  • 要打开任何文件,请使用:*启动"filename.format">

这应该会打开默认应用程序中的文件,如浏览器中的HTML文件、相册中的图像等。

最新更新