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

Reply via email to