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


The process of creating an exe that maintains the relative paththis 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.


Samer

Samer Kurdi has written 1068 awesome posts for Freewaregenius.

He has been reviewing software since 2006 when he started Freewaregenius.com

67 responses to “How to create shortcuts with a relative path for use on USB drives”

  1. Eviler

    Very nice! Just what i was looking for! Thank you.

  2. Norville Cole

    Hi,
    How do you include an option to change a init directory

    I:\gns3\GNS3.exe –configdir I:\GNS3

    Thanks
    Norville

  3. Martin Podbi

    Hi,
    This tutorial is great indeed. But I’ve got one question. How to run some program with additional parametrs. For example, I would like to have shortcut to “G:\NetBeans\bin\netbeans.exe” there is now trouble. But if i want run it with additional parameter “–console suppress” it won’t work. In my “bat” file I’ve placed the following: “NetBeans\bin\netbeans.exe –console suppress” and Windows command line won’t run it. It writes some message about don’t knowing if this is internal, or external program.
    Can anyone help?

    Thanks

  4. Abcd

    @Martin… try “”NetBeans\bin\netbeans.exe” “-console suppress”"

  5. Alex

    Thank you very much

  6. LouD

    Give this variation a try:

    This Batch-file is executed via a desktop shortcut AFTER a memory device is plugged into ANY available usb port.

    It searches possible drive letter assignments IN REVERSE ORDER from Z to G
    (excluding C D E & F) looking for the App (Bing.Bat) and executes it if it is found within that memory device on the specified path. The root directory In this case.

    Batchfile:
    FOR %%i in (Z Y X W V U T S R Q P O N M L K J I H G) DO (IF exist %%i:\BING.BAT %%i:\BING.BAT)

    or:

    Direct Command line:
    FOR %i in (Z Y X W V U T S R Q P O N M L K J I H G) DO (IF exist %i:\BING.BAT %i:\BING.BAT)

    About the icon:
    Find and copy the App’s icon from its normal location and put in with existing icons or change where the desktop shortcut should look for it.

  7. Eric

    Thanks for the tutorial. It is just what I was looking for. My experience:

    When I compiled and ran the new .exe, it left an unnecessary command prompt open. To solve this, I recompiled the .exe using the “invisible application” application. This is found in the Options tab in the Visibility box.

    Oddly, doing so slightly changed the behavior of the “User Account Control” window that pops up in Windows 7. The new behavior is slightly less favorable but I’m keeping it over the leftover command prompt window.

  8. Eric

    Correction: I recompiled the .exe using the “invisible application” option.

  9. Eric

    UPDATE:

    I found a better way to launch the application without a lingering command prompt. Format the .bat like this:

    start “” Path\Program.exe

    Run the .bat to make sure it works properly. When it does, use the Bat to Exe Converter to compile the .bat with the icon file as in the instructions in the original post. You could also skip this step and just use the .bat file as your shortcut if you don’t care about the icon. Sorry for the triple post.

  10. Jay

    Sweet!! Worked for me. Thanks dude.

Leave a Reply


seven × 1 =