确定打开给定文件扩展名的默认程序 - VBS

时间:2013-11-08 20:15:12

标签: windows vbscript

我发现了一些答案,但没有任何具体与我的问题有关,或与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

3 个答案:

答案 0 :(得分:2)

我最终决定使用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

答案 1 :(得分:1)

使用assoc

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

更新II:

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

答案 2 :(得分:0)

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

开头。

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

每个用户和每台计算机的协作,每个用户设置覆盖计算机设置。

在NT / Win 95中它被扩展了。所以HKCR.ext可以保存打开的字符串(\ shell \ open)以便与win.ini进行compat,但更常见的是使用文件类,例如HKCR.txt = txtfile。查找HKCR \ txtfile \ shell \ open为您提供了命令。

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

对于Word,它是使用DDE(也在上面的注册表项中指定)打开的,而不仅仅是命令行。那是它作为DDE服务器启动然后发送到它的fileopen命令与要打开的文件的名称

打开文件的新方法。

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

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

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