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

 

Description: 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. Before 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. Download 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.

发表评论