How to create shortcuts with a relative path for use on USB drives

The process of creating an exe that maintains the relative pathDescription: this posting will describe how to create program shortcuts on the root folder of a USB drive that preserve the relative path of the USB drive’s folder structure (such that these shortcuts will always work no matter what drive letter Windows assigns the drive). It will also aim to preserve the icon of the application in the new shortcut.

The problem: say you have a portable app that in a folder on a USB drive with, for example, a path such as “F:\EjectUSB\EjectUSB.exe”. Say, moreover, that you would like to create a shortcut to “EjectUSB.exe” and place it on the root folder of your USB (”F:\”, in this case). You might be tempted to simply copy a shortcut and place it on the root folder; except for one thing: that shortcut will always refer to “F:\EjectUSB\EjectUSB.exe”, and should you plug your USB drive into a computer that assigns a different letter to the USB drive (i.e. a letter than is not “F”) the shortcut will break down and not work. Hence the need for a shortcut with a relative path.

The solution:

  • Windows does not support relative paths in shortcuts. Instead, this article will describe a solution based on creating a so-called batch (.BAT) file and converting that into an executable (which nonetheless displays the original program’s icon).
  • I will use the example outlined above (”F:\EjectUSB\EjectUSB.exe”).

Step by step:

  1. Create a new text document (to create a text document right click anywhere on your desktop, select new, then select “Text Document” from the context menu). Open it and type in the path for the executable, between quotes, without the drive letter and colon (e.g. “\EjectUSB\EjectUSB.exe” - with the quotes). Save and exit. It does not matter what you name the file at this point, but let’s say you named it shortcut.txt for reference.
  2. Rename the text file and change the extension to .BAT instead of .TXT.
  3. BecyIconGrabber ScreenshotBefore we go further we have to extract the icon from the original app and save it as an ICO file. To do this use a program such as “BecyIconGrabber“. Download, extract, and run, then point to the original executable (”F:\EjectUSB\EjectUSB.exe” in this case). Right click on the icon when it appears (it doesn’t matter which size icon you are looking at) and save it as an ICO file.
  4. Bat to Exe shortcut screenshotDownload and unzip the free “Bat To Exe Converter” and run it. In the program dialog, point to the batch file you created (shortcut.bat) for the”batchfile” field and to the extracted ICO file for the “Iconfile” field. Use the “Compile” button to complete the operation.
  5. Two more things left to do (1) rename the new executable if you want to, and (2) move it to the root folder of your USB device.
  6. That’s it. You can now run the new exe file from the USB’s root folder to launch your program; it will always preserve the relative path, no matter what letter Windows assigns the USB device.

Related Posts

  • No Related Post
E-MAIL this
Subscribe

23 Comments so far

  1. [...] How to use: extract the EjectUSB folder onto your USB, and then when you are ready simply run the “EjectUSB.exe” executable to eject. If you would like to have a shortcut on your USB’s root folder that runs EjectUSB check out this posting. [...]

  2. fred on July 24th, 2008

    It’s when I see such things I’m happy to use a real 100% portable file manager.

    One of the so many excellent & addictive features of XYplorer (http://www.xyplorer.com) is its support for relative syntax. With that, when I use it, I can start application no matter what letter my USB stick got assigned, dbl-clicking on files like *.txt start my text editor without trouble either thanks to Portable File Associations…

    This thing is so great it’s hard to believe. I know it’s not free, but with a lifetime license it’s well-worth the money. And of course, you can always find the last freeware version if you look for it… ;)

  3. Samer on July 24th, 2008

    @Fred,
    Pegtop PStart is a launcher is freeware and it also supports relative paths:
    http://www.pegtop.net/start/

  4. Mark on July 25th, 2008

    Thanks, Samer. One question though. At some point I must have associated the BAT extension with Notepad, so now when I make the EXE and execute it, it just brings up my script in Notepad. How do I unassociate BAT files to get them to launch the script when the EXE files are executed?

  5. knityster on July 25th, 2008

    it’s perfect except this:
    when i run the application, a black DOS windows will show at the same time ..

    ^_^

  6. OAlexander on July 25th, 2008

    @ Mark

    >>>
    REGEDIT4

    [HKEY_CLASSES_ROOT\batfile\shell\edit]
    [HKEY_CLASSES_ROOT\batfile\shell\edit\command]
    @ = “Notepad.exe \”%1\”"
    [HKEY_CLASSES_ROOT\batfile\shell\open]
    [HKEY_CLASSES_ROOT\batfile\shell\open\command]
    @ = “\”%1\” %*”
    <<>> and <<< into a Textfile with Notepad, save it as something like “bat.reg”, double click it, and BATchfiles should have regained their magic.

  7. OAlexander on July 25th, 2008

    Did not quite work as intended above.

    @ Mark

    !!!
    REGEDIT4

    [HKEY_CLASSES_ROOT\batfile\shell\edit]
    [HKEY_CLASSES_ROOT\batfile\shell\edit\command]
    @ = “Notepad.exe \”%1\””
    [HKEY_CLASSES_ROOT\batfile\shell\open]
    [HKEY_CLASSES_ROOT\batfile\shell\open\command]
    @ = “\”%1\” %*”
    !!!

    Copy everything between the “!!!” (but without the “!!!”) into a Textfile with Notepad, save it as something like “bat.reg”, double click it, and BATchfiles should have regained their magic.

  8. Samer on July 25th, 2008

    @Mark,
    I am not sure what the issue is but seems like OAlexander above has the info you need.

    @knityser
    Yes the black DOS dialog will appear, but should only be there for a second or so.

  9. Rich on July 25th, 2008

    Samer,

    I think this guide is great: thank you.

    You can always assign a permanent letter to your USB — or any — drive in Windows.

  10. [...] can use batch files to quick-launch your USB workspace as soon as you plug in your thumb drive. How to create shortcuts with a relative path for use on USB drives [...]

  11. [...] can use batch files to quick-launch your USB workspace as soon as you plug in your thumb drive. How to create shortcuts with a relative path for use on USB drives [...]

  12. Arpit Jain on July 26th, 2008

    you could this nsis script i wrote to get a permanent letter on each pc you use your thumbdrive on just run it on any pc and you will get the letter you specified in the config file (if that letter is free) (Needs Administrator Privileges)

    http://portableapps.com/node/11892

    (i am known as tipra.wicked)

    (download the second launcher ie v2)

  13. Arpit Jain on July 26th, 2008

    sorry i didn’t check the link above
    http://rapidshare.com/files/132521065/ReMount.zip
    instructions Inside

  14. Anonymous on July 26th, 2008

    Nice Work

  15. anonymous coward on July 26th, 2008

    I’m happy (as always) that I use a decent operating system - one that uses a filesystem with support for relative symlinks. Oh, they have been in UNIX since 1983!
    A quick search on the web reveals that ntfs has something like symlinks, but they are poorly documented and hard to handle.
    And once you find yourself scripting around the deficencies of your operating-system, you’ll appreciate an OS with decent scripting support (Well, which family of OS could that be?).

  16. m^2 on July 26th, 2008

    This article is wrong, you CAN create relative shortcuts. No batches are needed.
    Sample: A .lnk that opens a.txt in own directory.

    MIME-Version: 1.0
    Content-Type: application/octet-stream; name=”a.txt.lnk”
    Content-Transfer-Encoding: base64
    Content-Disposition: attachment; filename=”a.txt.lnk”

    TAAAAAEUAgAAAAAAwAAAAAAAAEaKAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
    AAAAAQAAAAAAAAAAAAAAAAAAAAQAAAAFAGEALgB0AHgAdAA=

  17. AnotherApproach on July 27th, 2008

    You could achieve the same result in one step using a simple vbscript (.vbs) script file :-

    sDriveLetter = Left(Wscript.ScriptFullname,2)
    sRunCmd = sDriveLetter & “\folder\myprogram.exe”
    set oWS = WScript.CreateObject(”WScript.shell”)
    oWs.Run sRunCmd , 0, False

  18. Joe on July 28th, 2008

    An example of yet another approach using a default Windows shortcut (.lnk) file:

    To start siw.exe using a shortcut placed in the same directory, enter the following as the target:

    Win NT, 2000, XP, Vista: %windir%\system32\cmd.exe “/C start siw.exe”
    Win 95, 98, ME: %windir%\system32\start.exe “siw.exe”

    and then enter %cd% as the “Start In” directory.

    Set it to run minimized, and the flash of the CMD window will only show up in your task bar.

    You can use relative paths for “SIW/siw.exe” for a subdirectory, “../siw.exe” for the next-higher directory

  19. wm on July 29th, 2008

    this trick ALSO works on cd’s

  20. Wyatt on August 2nd, 2008

    THANKYOU SO MUCH!!!!!

  21. Ryan on August 5th, 2008

    This worked great for me. Thanks a ton!!! Also, I found if you run the Bat To Exe Converter and, above the “Compile” button, if you select “Ghost application” instead of “Console application”, no DOS Shell popped up. Knityster, you might want to try this.

  22. Rob on August 16th, 2008

    Seems to work pretty well, except that I ran it on the GUI for JkDefrag and it put the INI in the root of the drive.

    I find that the drive letter shifts to the correct letter if you use a shortcut anyway, though. Just doesn’t show the icon until the path changes.

  23. Stumpy on August 21st, 2008

    Joe’s Solution (5 up) is working great for me, and is the simplest.
    Change the double quotes if you cut and paste his example, as they come out funny.

Leave a reply