【问题标题】:Determine default program to open given file extension - VBS确定打开给定文件扩展名的默认程序 - VBS
【发布时间】:2013-11-20 23:28:11
【问题描述】:

我找到了一些答案,但没有一个专门针对我的问题,或者与 VBS 无关。

我正在寻找一种方法来在提供特定文件扩展名时确定默认程序的完整路径。

我的最终目标是自动创建任何程序打开“.DOC”文件(通常是 MS Word)的快捷方式。但这显然会在不同的 Windows 机器上有所不同。

我很想做这样的事情:

strDefaultDOCProgram = WshShell.FindAssociatedProgram("doc")

在哪里

strDefaultDOCProgram = "C:\Program Files\Microsoft Office 15\root\office15\winword.exe"

也许有帮助? Ask Windows 7 - what program opens this file by default

【问题讨论】:

    标签: windows vbscript


    【解决方案1】:

    使用关联

    assoc /?
    Displays or modifies file extension associations
    
    ASSOC [.ext[=[fileType]]]
    
      .ext      Specifies the file extension to associate the file type with
      fileType  Specifies the file type to associate with the file extension
    

    和ftype

    type /?
    isplays or modifies file types used in file extension associations
    
    TYPE [fileType[=[openCommandString]]]
    
     fileType  Specifies the file type to examine or change
     openCommandString Specifies the open command to use when launching files
                       of this type.
    

    喜欢

    assoc .doc
    .doc=OpenOffice.org.Doc
    
    ftype OpenOffice.org.Doc
    OpenOffice.org.Doc="C:\Program Files\OpenOffice.org 3\program\\swriter.exe" -o "%1"
    

    通过使用 .Exec 执行这些程序的脚本。

    更新:

    从命令中剪切文件规范:

    >> sCmd = """C:\Program Files\OpenOffice.org 3\program\\swriter.exe"" -o ""%1"""
    >> WScript.Echo sCmd
    >> WScript.Echo Split(sCmd, """")(1)
    >>
    "C:\Program Files\OpenOffice.org 3\program\\swriter.exe" -o "%1"
    C:\Program Files\OpenOffice.org 3\program\\swriter.exe
    

    更新二:

    不要使用 .RegRead 来尝试在本周版本的注册表中查找信息; assoc 和 ftype 是您的操作系统为您的问题提供的工具。

    【讨论】:

    • 我会玩这个,但看起来很有希望!有没有办法只获取路径而不需要任何额外的标志?
    • 谢谢!我会 +1,但还不能;)
    • 使用操作系统的内置工具无疑是更好的实践和更具可扩展性。然而,在我的例子中,使用.RegRead 被证明比从assocftype 捕获输出更短更容易,然后无论如何都必须拆分输出。我只需要 Windows 7 机器,但如果其他人有更大的样本量,那么他们可能应该使用内置工具
    • 仅供参考,我使用它的方式如下:Dim WshShell, strProg, strProgPath Set WshShell = CreateObject("WScript.Shell") strProg = WshShell.RegRead("HKCR\.doc\") strProgPath = WshShell.RegRead("HKCR\" & strProg & "\Protocol\StdFileEditing\Server\")
    【解决方案2】:

    打开文件的方式随着时间的推移而改变。您在谈论打开文件的传统方式,这仍然是最常见的。

    开始。

    为了支持办公,您可以输入 win.ini *.doc=c:\winword.exe。

    关联是针对每个用户和每台机器的,每个用户的设置优先于机器设置。

    在 NT/Win 95 中它被扩展了。因此 HKCR.ext 可以保存打开字符串 (\shell\open) 以与 win.ini 兼容,但更典型的是指向文件类,例如 HKCR.txt=txtfile。查找 HKCR\txtfile\shell\open 给你命令。

    由于程序窃取了文件关联,因此现在有一层其他关联覆盖它。因此,该命令是从上面构建的,这些较新的键 HKEY_CLASSES_ROOT\SystemFileAssociations(其中还包括通用文件类型的新概念的关联 - 图片或音乐)或 HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts。

    对于 Word,它是使用 DDE(也在上述注册表项中指定)打开的,而不仅仅是命令行。也就是说,它作为 DDE 服务器启动,然后将带有要打开的文件名的 fileopen 命令发送给它

    打开文件的新方法。

    现在使用 COM 打开文件。程序在上述键下注册 IDropTarget。

    上下文菜单处理程序可以覆盖上述内容。他们也在上面注册。

    最好的方法是 shellexec 文件。它会像双击一样打开。

    【讨论】:

      【解决方案3】:

      我最终决定使用assocftype 命令,以防万一我们想在任何其他Windows 版本上使用此脚本。这是一个可以完成我需要的一切的函数。我希望它对某人有帮助!

      Dim WshShell
      Set WshShell = CreateObject("WScript.Shell")
      
      ' Should supply the program extension with period "." included
      Function GetProgramPath(ext)
      Dim strProg, strProgPath
      
      ' Get Program Association Handle
      Set oExec =  WshShell.Exec("cmd.exe /c assoc " & ext)
      strProg = oExec.StdOut.ReadLine()
      strProg = Split(strProg, "=")(1)
      
      ' Get Path To Program
      Set oExec =  WshShell.Exec("cmd.exe /c ftype " & strProg)
      strProgPath = oExec.StdOut.ReadLine()
      strProgPath = Split(strProgPath, """")(1)
      
      ' Return the program path
      GetProgramPath = strProgPath
      
      End Function
      
      strPath = GetProgramPath(".doc")
      WScript.Echo strPath
      

      【讨论】:

      猜你喜欢
      • 2012-02-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-21
      • 2015-05-27
      • 1970-01-01
      • 2019-08-28
      相关资源
      最近更新 更多