By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,526 Members | 1,888 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,526 IT Pros & Developers. It's quick & easy.

How to get the target of a Windows shortcut file

P: n/a
Below is some code adapted from something I think was written by Mark
Hammond. Originally I needed to create a Windows shortcut (link), and
this code does the trick, requiring only the target filename and the
desired shortcut name.
Now, I find I need to open a shortcut and extract the target filename,
and I don't have a clue how that is achieved. To be clear, I mostly
don't understand the gory (Windows API) details of this code. So, can
anyone show how to open an existing shortcut file (given its name) and
discover the name of the file to which it is a shortcut?
import os
from win32com.shell import shell
import pythoncom

# Get the shell interface.
sh = pythoncom.CoCreateInstance(shell.CLSID_ShellLink, None, \
pythoncom.CLSCTX_INPROC_SERVER, shell.IID_IShellLink)

# Get an IPersist interface
persist = sh.QueryInterface(pythoncom.IID_IPersistFile)

target_of_link = os.path.abspath('target.doc')
link_name = 'shortcut_to_target.doc.lnk'

sh.SetPath(target_of_link)
persist.Save(link_name, 1)

May 5 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a
persist.Load('someshortcut.lnk')
print sh.GetPath(shell.SLGP_RAWPATH)[0]

hth
Roger

"Steve M" <sj******@gmail.com> wrote in message news:11**********************@i39g2000cwa.googlegr oups.com...
Below is some code adapted from something I think was written by Mark
Hammond. Originally I needed to create a Windows shortcut (link), and
this code does the trick, requiring only the target filename and the
desired shortcut name.
Now, I find I need to open a shortcut and extract the target filename,
and I don't have a clue how that is achieved. To be clear, I mostly
don't understand the gory (Windows API) details of this code. So, can
anyone show how to open an existing shortcut file (given its name) and
discover the name of the file to which it is a shortcut?
import os
from win32com.shell import shell
import pythoncom

# Get the shell interface.
sh = pythoncom.CoCreateInstance(shell.CLSID_ShellLink, None, \
pythoncom.CLSCTX_INPROC_SERVER, shell.IID_IShellLink)

# Get an IPersist interface
persist = sh.QueryInterface(pythoncom.IID_IPersistFile)

target_of_link = os.path.abspath('target.doc')
link_name = 'shortcut_to_target.doc.lnk'

sh.SetPath(target_of_link)
persist.Save(link_name, 1)


----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
May 6 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.