[python-win32] How to read the target of a Windows Shortcut

Ayaskanta Swain ayaskanta.swain at altair.com
Fri Mar 4 12:59:03 CET 2011


Hi, 

 

I want to know the target path of a windows shortcut. How do i read the
short cut & retrieve the actual target path it is pointing to? I
searched the internet & all the documents I found there refer to use
Win32com.shell or win32com.client module but was not satisfied with the
solutions given there.

 

I don't want to create a new shortcut or update an existing one. What I
want is to read the target location from an existing shortcut. The
shortcut can be anywhere in my computer & not necessarily on desktop.
For example - C:\temp\my_shortcut.lnk which is pointing to
D:\john\temp\myfile.txt file. Here i can pass the shortcut path
"C:\temp\my_shortcut.lnk" to my python script & then i want to retrieve
the target file's path from that shortcut. Is there anything equivalent
of os.readlink(symlink) which is used on Unix systems? Is there any way
to do that using win32com.shell modules? 

 

The following code works if the target file exists & hence returns me
the absolute path of the target, but fails if the shortcut is broken i.e
if the target file has been removed. In the later case this code creates
a target file of zero (0) size with the same path. This is bad .I want
to detect the broken shortcut case. 

 

      shell = win32com.client.Dispatch("WScript.Shell")

      shortcut = shell.CreateShortCut(absolutepath)

 

Any help to solve this issue will be highly appreciated.

 

Regards

Ayaskant

Bangalore

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-win32/attachments/20110304/676de5fa/attachment.html>


More information about the python-win32 mailing list