Windows .LNK shortcut files, start application using relative rather than absolute paths.

/ Published in: DOS Batch
Save to your folder(s)

.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:
[1] On USB flash drives or CDs, start an app in a sub-directory.
[2] 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.

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:
[1] 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.
[2] shortcut.exe or scut.exe (3rd party tools I haven't tried)
[3] 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.
[4] menu utilities intended for USB flash drives (I haven't tried)

How to modify the .LNK shortcut:
[1] right click on the shortcut. Expect context menu. Select Properties. Expect Properties dialog.
[2] Set 'Run Minimized' otherwise a black CMD window will briefly appear when the .LNK is used.
[3] Set target to:
cmd.exe /c start /d. ..\a\a.txt
where ..\a\a.txt represents a relative path to your target.
[4] Optionally set an icon.
[5] '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.

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:'

Copy this code and paste it in your HTML
  1. Add the following to a Windows .LNK shortcut. This example launches the .TXT editor. Note the dots - there are 4 shown.
  3. %windir%\system32\cmd.exe /c start /d. ..\parentDir\some.txt

Report this snippet


RSS Icon Subscribe to comments

You need to login to post a comment.