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:
- 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.
- Rename the text file and change the extension to .BAT instead of .TXT.
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.
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.- 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.
- 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 |
|
|
|
|
|
|
|
E-MAIL this |
|
Subscribe |

E-MAIL this
Loading ...
[...] 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. [...]
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…
@Fred,
Pegtop PStart is a launcher is freeware and it also supports relative paths:
http://www.pegtop.net/start/
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?
it’s perfect except this:
when i run the application, a black DOS windows will show at the same time ..
^_^
@ 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.
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.
@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.
Samer,
I think this guide is great: thank you.
You can always assign a permanent letter to your USB — or any — drive in Windows.
[...] 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 [...]
[...] 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 [...]
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)
sorry i didn’t check the link above
http://rapidshare.com/files/132521065/ReMount.zip
instructions Inside
Nice Work
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?).
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=
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
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
this trick ALSO works on cd’s
THANKYOU SO MUCH!!!!!
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.
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.
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.