How to get the target of a Windows shortcut file

Discussion in 'Python' started by Steve M, May 5, 2006.

  1. Steve M

    Steve M Guest

    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)
    Steve M, May 5, 2006
    #1
    1. Advertising

  2. Steve M

    Roger Upole Guest

    persist.Load('someshortcut.lnk')
    print sh.GetPath(shell.SLGP_RAWPATH)[0]

    hth
    Roger

    "Steve M" <> wrote in message news:...
    > 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 =----
    Roger Upole, May 6, 2006
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Gabstar
    Replies:
    4
    Views:
    8,369
    Oliver Wong
    May 30, 2006
  2. Ernesto

    Run Windows shortcut

    Ernesto, Feb 3, 2006, in forum: Python
    Replies:
    1
    Views:
    337
    Ernesto
    Feb 3, 2006
  3. Ganesh
    Replies:
    2
    Views:
    338
    Ganesh
    Jul 12, 2004
  4. MVB
    Replies:
    3
    Views:
    505
    Richard Heathfield
    Aug 13, 2007
  5. mohit
    Replies:
    0
    Views:
    532
    mohit
    Jan 23, 2008
Loading...

Share This Page