Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to programmatically find target of a shortcut. 1

Status
Not open for further replies.

JRJohnston

Programmer
Sep 8, 2000
4
US
I would lilke to, in a VB program, VB script, or any other way, find the target of a shortcut.

My experience with VBScript FileSystemObject & VB FileSystemObject is that shortcuts are not supported. (Not even the filetype code for shortcuts is supported.)

If that is not correct, I would like to know.
But, most importantly, in what programming environments is it possible to open
a shortcut (*.lnk file), and determine the pathname of the target? And, how?

Thanks a bunch.
Jim

P.S. I think I have posted something like this previously, but I cannot now find it.
JRJ

 
I've never actually worked with shortcuts but I've seen the C++ documentation for the IShellLink interface. It has a 'GetPath' method that can be used to get a shortcut's path.

If you manage to create a VB wrapper for this it would make a great FAQ to post.
VBSlammer
redinvader3walking.gif
 
That is an excellent link - works great. Here's what I tried:
Code:
Public Function GetShortcutPath(ByVal strShortcutName As String) As String
On Error GoTo ErrHandler

  Dim sDesk As String
  Dim oShell As New IWshShell_Class
  Dim oShortCut As New IWshShortcut_Class

  sDesk = oShell.SpecialFolders.Item("Desktop")
  Set oShortCut = oShell.CreateShortcut(sDesk & "\" & strShortcutName & ".lnk")

  GetShortcutPath = oShortCut.TargetPath

ExitHere:
  On Error Resume Next
  Set oShell = Nothing
  Set oShortCut = Nothing
  Exit Function
ErrHandler:
  MsgBox Err & "-" & Err.Description
  Resume ExitHere
End Function

Be sure to reference the 'Windows Script Host Object Model.'
VBSlammer
redinvader3walking.gif
 
*** Note these other arguments for 'SpecialFolders' ***
Code:
0  or "Desktop"
1  or "StartMenu"
2  or "Programs"
3  or "Startup"
5  or "AppData"
6  or "PrintHood"
7  or "Templates"
8  or "Fonts"
9  or "NetHood"
12 or "SendTo"
13 or "Recent"
15 or "Favorites"
16 or "MyDocuments"
[code] [b][COLOR=blue]VBSlammer[/color][/b][img]http://users3.ev1.net/~slamkeys/images/redinvader3walking.gif[/img]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top