Posted By

jimfred on 05/13/10


Tagged


Versions (?)

Who likes this?

3 people have marked this snippet as a favorite

flystill
Tyster
thastyle


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


 / Published in: DOS Batch
 

.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.

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: [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.

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

  1. Add the following to a Windows .LNK shortcut. This example launches the .TXT editor. Note the dots - there are 4 shown.
  2.  
  3. %windir%\system32\cmd.exe /c start /d. ..\parentDir\some.txt

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: flystill on May 24, 2010

So cute~ So useful~ Is “/d." necessary? I tried the command below which seems OK. cmd.exe /c start ChildDir/Filename.***

Posted By: jimfred on June 13, 2010

Thanks - you're right - the /d. apparently isn't necessary.

Posted By: jimfred on June 25, 2010

Relative paths don't work on UNC paths (e.g., \myNas\public). An absolute UNC path works but a relative UNC path does not.

Posted By: sxmanzano on June 30, 2012

Muy buen aporte, Gracias; Complemento para los que esten batallando en español:

1.-(Esto va en DESTINO o PATH notese: El & ,las comillas y los puntos con espacio)

%windir%\system32\cmd.exe /c start /d&". ..\alguna carpeta\el documento.txt"

2.-(INICIAR EN o START IN pueden quedar en blanco)

3.-(EJECUTAR o EXECUTE = MINIMIZADO ...para que no se vea el pantallazo negro del cmd

You need to login to post a comment.