.LNK files only support absolute paths even when using IWshRuntimeLibrary.IWshShortcut.
Early versions of Windows, such as XP, don't support symLinks unless CygWin is used.
Q: How can a .LNK be created to start an app 'nearby' via a relative path?
A: Use a regular .LNK shortcut file and pre-pend "cmd.exe /c start /d. " before the command.
The 'cmd.exe' (should be in the path) starts a cmd window (similar to a .BAT file) The '/c' (passed to cmd.exe) carries out the command and then terminates async. The 'start' allows cmd to terminate - otherwise a cmd window would remain open. The '/d .' (note the dot) will use the current directory as the default directory.
Example usage:  On USB flash drives or CDs, start an app in a sub-directory.  In software project directories, create for example, projectFile.lnk to point to the .SLN file. Such a .LNK file could be checked-in to SVN or SourceSafe etc.
Variations: Using a command of . (a single dot) will open a cmd window in the current directory where the .LNK is. The '&' cmd operator can string several commands together.
Other options:  batch file. With or without 'Bat To Exe Converter'. In fact, the only advantages of .LNK over a .BAT is that the .LNK doesn't pop-up a black CMD window (if Run Minimized is used) and an icon can be used.  shortcut.exe or scut.exe (3rd party tools I haven't tried)  hex-edit the binary .LNK shortcut file (I haven't tried). Renaming and editing a .LNK can be tricky. The proprietary binary format is subject to change.  menu utilities intended for USB flash drives (I haven't tried)
How to modify the .LNK shortcut:  right click on the shortcut. Expect context menu. Select Properties. Expect Properties dialog.  Set 'Run Minimized' otherwise a black CMD window will briefly appear when the .LNK is used.  Set target to: cmd.exe /c start /d. ..\a\a.txt where ..\a\a.txt represents a relative path to your target.  Optionally set an icon.  'Start in' can be blank. Note: Windows will automatically replace, say, cmd.exe with C:\Windows\system32\cmd.exe which could break on another PC so, instead, use %windir%\system32\cmd.exe.
Rant: Symlinks are simpler, proven and intuitive. It's a major annoyance that the 'helpful' auto-modification of the .LNK by Windows complicates an otherwise conceptually simple objective. Microsoft finally added symlinks to later versions of NTFS which begs the question: what took so long especially since a good working model existed and could have been plagiarized earlier?
This all becomes unnecessary with newer version of NTFS (after XP's version of NTFS) because newer versions of NTFS have symlinks. Somewhat related, in XP, this command shows the NTFS version: 'fsutil fsinfo ntfsinfo c:'
- Add the following to a Windows .LNK shortcut. This example launches the .TXT editor. Note the dots - there are 4 shown.
- %windir%\system32\cmd.exe /c start /d. ..\parentDir\some.txt
CommentsSubscribe to comments
You need to login to post a comment.