Well, Here is how I have implemented in my script. Please take a look & let me know whether I am doing something wrong. Mine is a Windows-XP(SP3), 32-bit system.
# Reads the windows shortcut & returns the target path def shortcut_target (shortcutfile): link = pythoncom.CoCreateInstance ( shell.CLSID_ShellLink, None, pythoncom.CLSCTX_INPROC_SERVER, shell.IID_IShellLink ) link.QueryInterface (pythoncom.IID_IPersistFile).Load (shortcutfile) target_path, _ = link.GetPath (shell.SLGP_UNCPRIORITY) return target_path #############################Main####################################### ## linkpath = sys.argv[1] #Read the target of the windows shortcut target_of_link = shortcut_target(linkpath) print 'Target = ' + target_of_link I run the script like this C:\Documents and Settings\pbsadmin>python filelinkscript.py "C:\temp\myfile.lnk" Here myfile.lnk is a link to a file C:\stage\myfile.txt Thanks Ayaskant- -----Original Message----- From: python-win32-bounces+ayaskanta.swain=altair....@python.org [mailto:python-win32-bounces+ayaskanta.swain=altair....@python.org] On Behalf Of Tim Golden Sent: Friday, March 04, 2011 8:01 PM Cc: python-win32@python.org Subject: Re: [python-win32] How to read the target of a Windows Shortcut On 04/03/2011 12:27, Ayaskanta Swain wrote: > Thank you for sending me the link. I tried it by copying the > "shortcut_target(filename)" function into my script. But it does the > same thing as I had mentioned in my previous e-mail. i.e It returns me > the target path correctly if the target exists physically in my hard > disk, but behaves in a wrongful manner if I delete the target keeping > the shortcut intact (Broken shortcut). In that case it creates an empty > file (0 size) with the same name as that of the existing target & > creates it at the same physical location where the target was existing > before. Looks like a bug in this function. Well I'm not seeing that behaviour at all. Is it possible that something else in your code is having this effect? I'm on Win7 x64 Python 2.6.6 pywin32 216 <code> import os, sys import glob import pythoncom from win32com.shell import shell, shellcon def shortcut_target (filename): link = pythoncom.CoCreateInstance ( shell.CLSID_ShellLink, None, pythoncom.CLSCTX_INPROC_SERVER, shell.IID_IShellLink ) link.QueryInterface (pythoncom.IID_IPersistFile).Load (filename) # # GetPath returns the name and a WIN32_FIND_DATA structure # which we're ignoring. The parameter indicates whether # shortname, UNC or the "raw path" are to be # returned. Bizarrely, the docs indicate that the # flags can be combined. # name, _ = link.GetPath (shell.SLGP_UNCPRIORITY) return name if __name__ == '__main__': # # Create a dummy file # import tempfile filepath = tempfile.mktemp () with open (filepath, "wb") as f: f.write ("abcdef") # # Create a shortcut on the desktop to the file # shortcut = pythoncom.CoCreateInstance ( shell.CLSID_ShellLink, None, pythoncom.CLSCTX_INPROC_SERVER, shell.IID_IShellLink ) shortcut.SetPath (filepath) shortcut_path = os.path.join (shell.SHGetFolderPath (0, shellcon.CSIDL_DESKTOP, 0, 0), "test.link") shortcut.QueryInterface (pythoncom.IID_IPersistFile).Save (shortcut_path, 0) # # Check that the shortcut target is the file # and that the file exists # assert os.path.exists (filepath) assert shortcut_target (shortcut_path).lower () == filepath.lower () # # Remove the target file # os.unlink (filepath) assert not os.path.exists (filepath) # # Check that the shortcut target is the file # and that the file no longer exists # assert shortcut_target (shortcut_path).lower () == filepath.lower () assert not os.path.exists (filepath) </code> TJG _______________________________________________ python-win32 mailing list python-win32@python.org http://mail.python.org/mailman/listinfo/python-win32
_______________________________________________ python-win32 mailing list python-win32@python.org http://mail.python.org/mailman/listinfo/python-win32