Desktop shortcuts and target paths
Today I learned about an annoying property of Windows desktop shortcuts.
I need to access a Windows desktop shortcut that one of our applications installs, and modify its TargetPath property to make it point to a slightly different location. The shortcut is implemented as a .lnk but points to a web page. This is not the standard way of creating web shortcuts but it does work. However, the TargetPath property of such a shortcut appears to not be accessible in the usual way.
For example, the following VBScript code will either create a shortcut (or update it if it already exists) then display the TargetPath property of the shortcut:
Dim WshShell, objLnk, strLnk
strLnk = "C:Documents and SettingsmyuserDesktopTest.lnk"
Set WshShell = CreateObject("WScript.Shell")
Set objLnk = WshShell.CreateShortcut(strLnk)
objLnk.Description = "Test Me"
objLnk.TargetPath = "C:Tests"
objLnk.Save
Msgbox(objLnk.TargetPath)
Set objLnk = Nothing
Set WshShell = Nothing
But the following code returns an empty TargetPath:
Dim WshShell, objLnk, strLnk
strLnk = "C:Documents and SettingsmyuserDesktopTest.lnk"
Set WshShell = CreateObject("WScript.Shell")
Set objLnk = WshShell.CreateShortcut(strLnk)
objLnk.Description = "Test Me"
objLnk.TargetPath = "http://www.google.com"
objLnk.Save
Msgbox(objLnk.TargetPath)
Set objLnk = Nothing
Set WshShell = Nothing
Both shortcuts work, however.
It seems that either VBScript or WScript is trying to be too smart for its own good, and is ignoring the TargetPath path for a LNK file that goes to a web page rather than file path.