Reputation: 75
There are a handful of answers I've found but nothing that deals specifically with my problem exactly, or not with VBS.
I am looking for a way to determine the full path to a the default program when providing a specific file extension.
My ultimate goal is to automatically create a shortcut to whatever program opens ".DOC" files (typically MS Word). But this will obviously vary on different windows machines.
I would love to do something like:
strDefaultDOCProgram = WshShell.FindAssociatedProgram("doc")
where
strDefaultDOCProgram = "C:\Program Files\Microsoft Office 15\root\office15\winword.exe"
Maybe helpful? Ask Windows 7 - what program opens this file by default
Upvotes: 1
Views: 6596
Reputation: 75
I ultimately decided to use the assoc
and ftype
commands just in case we want to use this script on any other Windows versions. Here's a function that will do everything I needed. I hope it's helpful to someone!
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
Upvotes: 3
Reputation: 38745
use 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
and 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.
like
assoc .doc
.doc=OpenOffice.org.Doc
ftype OpenOffice.org.Doc
OpenOffice.org.Doc="C:\Program Files\OpenOffice.org 3\program\\swriter.exe" -o "%1"
via a script that executes those programs with .Exec.
Update:
Cut the file spec from the command:
>> 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
Update II:
Don't use .RegRead to try to find the info in this week's version of the registry; assoc and ftype are the tools your operating system provides for your problem.
Upvotes: 2
Reputation: 743
The means for opening files has changed over time. You are talking about legacy ways of opening a file, which is still the most common.
The beginning.
To support office you could put in win.ini *.doc=c:\winword.exe.
Asociations are per user and per machine with per user settings overriding machine settings.
In NT/Win 95 it was expanded. So HKCR.ext could hold the open string (\shell\open) for compat with win.ini but more typically poined to a file class, eg HKCR.txt=txtfile. Looking up HKCR\txtfile\shell\open gave you the command.
Due to programs stealing file associations this now has an layer of other associations put over it. So the command is built from above and these newer keys HKEY_CLASSES_ROOT\SystemFileAssociations (which also includes associations for a newer concept of a general type of file - picture or music) or HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts.
For Word, it was opened using DDE (also specified at the above registry keys) rather than just a command line. That is it was started as a DDE server then had fileopen commands sent to it with the name of the file to be opened
New Ways of Opening Files.
Files are now opened using COM. The program registers IDropTarget under above keys.
Context menu handlers can override the above. They are also registered above.
The best way is to shellexec the file. It will just open like it was double clicked.
Upvotes: 0