Home » Articles, File & Dir Utils, System, Utilities

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

Submitted by Samer on July 24, 2008 – 3:43 pm39 Comments

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.

39 Comments »

  • [...] 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. [...]

  • fred says:

    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… ;)

  • Samer says:

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

  • Mark says:

    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?

  • knityster says:

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

    ^_^

  • OAlexander says:

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

  • OAlexander says:

    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.

  • Samer says:

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

  • Rich says:

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

  • Arpit Jain says:

    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)

  • Arpit Jain says:

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

  • Anonymous says:

    Nice Work

  • anonymous coward says:

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

  • m^2 says:

    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=

  • AnotherApproach says:

    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

  • Joe says:

    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

  • wm says:

    this trick ALSO works on cd’s

  • Wyatt says:

    THANKYOU SO MUCH!!!!!

  • Ryan says:

    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.

  • Rob says:

    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.

  • Stumpy says:

    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.

  • dave says:

    When I do this, the command doesn’t know that it has opened the program so the script stays open. Any thoughts?

  • Alex says:

    Doesn’t work for me. My batch file reads:
    “\PDFV_Portable\PDFXCview.exe”
    (quotes included)
    Upon executing it, it launches PDF Viewer as expected.
    After “compiling” it to EXE, I get a flashing window. Both EXE and BAT are in T:\

    If I run the exe from the command prompt, I get the following result:
    C:\DOKUME~1\Alex\LOKALE~1\Temp\232.tmp>”\PDFV_Portable\PDFXCview.exe”
    Das System kann den angegebenen Pfad nicht finden.

    First line suggests that the exe is launched from a temp directory (and not from t:\ where the exe resides)
    Second line says that the system could not find the specified path (i.e. \PDFV_Portable), which is understandable, given the fact that it starts from C:\ and not T:\
    So, while the problem is clear (program is executed from a different place), I don’t know the solution.

  • [...] is een perfecte en relatief eenvoudige oplossing, die ik heb gevonden op Freewaregenius.com. Je hebt 2 eenvoudige gratis tooltjes nodig. Eén om het ikoontje van de toepassing op te halen uit [...]

  • suuk says:

    when i tried to make the .bat file executable, my antivirus jumped right over it screaming it was a trojan horse. i don’t like this solution!

  • ran says:

    what if i want to run some .exe with command line?
    like “abc\game” -console

  • [...] To get around this problem, freewaregenius.com has a nice solution using a batch file and converting it to an executable. For the complete how-to, check out How To Create Shortcuts For Use On USB Drives. [...]

  • Firestorm says:

    Help please.
    Won’t work for Launchy.
    Here’s what I typed in:
    “\PortableApps\Launchy\Launchy.exe”

    I tested it with the portableapps menu and that one works
    This is what I typed in for that one:
    “\PortableApps\PortableAppsMenu\PortableAppsMenu.exe”

    I’m stumped, why won’t ot work for launchy? The only dif is the last folder name and obviously the exe . I can run launchy manualy from there or from the portableapps menu but not from the bat/exe. :’(

  • Jedi_Master_Revan@yahoo.ca says:

    The scary part is, though I’ll never use this for what the article explains, but I’ll probably find a hundred other uses. Especially the icon thing. Thanks :)

  • Moman256 says:

    Ok, if you guys don’t want a black DOS windows to pop up. Then, when you are making the conversion, check invisible application instead of visible application (the default)

  • Vladimir Orlovsky says:

    I do it in a much more simpler way:
    RelativePathForUSB.1.bat ->
    start \usb\T\Test\Test1\Test1.txt

    ‘usb’ is a main directory.
    You can store *.bat files in any directory on your usb drive, .. it will find any file/application on your usb drive.
    Thanks, Vladimir Orlovsky

  • Adam says:

    I have improved on this slightly so that you can use command line options with the batch file, and pass it on. This also works with spaces, you just need to enclose them with quotations.

    Simply use this template, and replace the 4th line down with your path from above, remembering the %* at the end.

    @echo off
    set count=0
    call :loop %*
    “Program Files\Support Tools\setx.exe” %*
    goto :EOF
    :loop
    if !%1!==!! goto :EOF
    shift
    set /a count+=1
    goto :loop

  • Adam says:

    My mistake, I pasted the debug code. All you need is:

    @echo off
    “Program Files\Support Tools\setx.exe” %*

  • Help? says:

    on some public computers the cmd is disabled and you cant use it. any way to get past that?

  • James says:

    Best comment by Joe. This works flawlessly; no bat files, no extra applications to download. Of course, I only need these shortcuts for Vista/Win7. I have an autorun.inf in the root for shell integration in XP. (Best to mark it as read only.)


    shell=0cmd
    shellcmd=CMD NAME
    shellcmd\command=explorer \PATH\Path
    #clicking on the drive launches a window to specified folder.

    shell=1KPX
    shell\1KPX=1. KeePassX
    shell\1KPX\command=PATH\Path\KeePassX.exe
    #right click drive, click on '1. KeePassX' to launch KeePassX

    shell=2True
    shell\2True=2. TrueCrypt
    shell\2True\command= PATH\Path\TrueCrypt.exe /v PATH\Path\volume
    #right click drive, click on '2. TrueCrypt' to launch TrueCrypt to a
    #specified volume.

    shell=3dismounttc
    shell\3dismounttc=3. Dismount
    shell\3dismounttc\command=PATH\Path\TrueCrypt.exe /d /q
    #right click drive, click on '3. Dismount' to dismount TrueCrypt
    #volumes and exit program.

  • CMedina says:

    I’ve found another way.. it is pretty simple but will leave you a cmd window opened until main application is closed.

    1. Create a Shortcut

    2. Just write in “Traget:
    %windir%\system32\cmd.exe /c cd “__RELPATH_TO_MY_WORKING_DIR__” & “__APP_NAME__”

    3. In the “Start In:” (Leave it blank)

    Note:
    I dont know if using the command
    cd approach will work for you as “Start In:” does but it worked for me.

    Also, you can set “Run:” to minimized but will leave window still opened… I hate that window!

  • Wouter Lievens says:

    This is not good enough, unfortunately. Virus scanners will not pick up on the shortcut, but they will complain about the executable you create :-(

Leave a comment!

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

This is a Gravatar-enabled weblog. To get your own globally-recognized-avatar, please register at Gravatar.