473,399 Members | 3,919 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

How to get the target of a Windows shortcut file

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
1 7964
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: md | last post by:
Hi everybody - I am writing an application to change the target and start in folder in shortcuts. I am using Mattias Sjögren's ShellLink library (thanks!) but I found a problem I was hoping...
3
by: Stanislaw Findeisen | last post by:
Does anyone know how to create file shortcuts in Windows? The only way I know is like: --------------------------------------------------------------- import win32com.client ...
2
by: Ganesh | last post by:
I have to change the tartget property of a windows start->Program->X's shortcut properties using c program. Do we have any function for that ? please reply me ASAP.. as it is urgent !
9
by: hhh12347 | last post by:
FolderBrowserDialog crashes on my Windows 2000 computer. Here is a C# test program: using System; using System.Windows.Forms; public class TestForm : Form { FolderBrowserDialog...
0
by: Damir Dezeljin | last post by:
Hi. I'm using CoCreateInstance(CLSID_ShellLink, ...) to create an shortcut from my program. Later I have to open the shortcut target file. Is there an easy way to open it? Something like...
0
by: Ray | last post by:
I am creating a deployment project for my application and when I select "Target" for my application shortcut the "Select Folder" dalog is displayed instead of the "Select item in project" dialog...
2
by: Rob R. Ainscough | last post by:
I've setup a Windows Installer Setup Project for my VS 2005 VB.NET app -- it installs find, but my Desktop icon and Programs Menu icon do not run the app ..exe when I click on them -- I can't...
3
by: MVB | last post by:
Hello All, I want to get the Target Path of shortcut file on Windows using C. Can anyone help me in this? Thanks & Regards, MVB
3
by: Richard Townsend | last post by:
If I have a windows shortcut to a URL, is there a way to get the URL in a Python app? I found some code that uses pythoncom to resolve shortcuts to local files, but I haven't found any examples...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.