Για τους μη αγγλομαθείς, θα ακολουθήσει και ελληνικό κείμενο

For quite some time, i was wondering if there was an easy way to have relative path shortcuts in windows – for use on usb sticks, external drives, etc.

The problem starts when you have a program you need to run from your usb stick – but while inserting it on other computers, the assigned letter changes, therefore the path also. Windows work on an absolute path (ie F:\Program Folder\Program.exe) but the F:\ part on someone else’s computer may become D:\ or E:\ – and that will be ruing the shortcut.

While doing a research on the mighty Google, i came up to hard answer: While many people had tried, relative program shortcuts seemed not possible.

Others, used two or three programs to make it work, but this was not a plug-and-play; others, created programs to run relative path shortcuts.

But yesterday, i had a small idea: how did my computer know where is the notepad program? The question is not all that simple, because due to a bizzare installation, my local drive is not the normal C:\ but F:\

Well, all i had to do was look the shortcut of the program. Right Click on the icon, Properties, and i was right. The program was not located on C:\ or F:\ but on a %SystemRoot% variable.

I was on to something. What did the variables do? Well, i just had to ask Wikipedia: http://en.wikipedia.org/wiki/Environment_variable

Now, my usb program was not in %SystemRoot%, nether in %SYSTEMDRIVE% – those were variables locating to absolute paths. So?

Well, i start looking around for other variables. And the %path% variable seemed quite to the place. While it does not say anything like that on the manuals, i thought that the %path% variable would return the actual path of the shortcut. So, if i used that plus the location from the shortcut to the program, it would work regardless in which folder or drive the shortcut was placed.

So, i’ve created a shortcut for my program, I’ve put it on the root of my usb drive, then went to its properties (right click, properties) and changed the ‘Start In:’ location from D:\Program File\ to %path%\Program File\Program.exe (note that i added the ‘program.exe’)

When i tested the usb on an other computer, with a different drive letter, the shortcut worked like a charm 🙂

The obvious drawback here is that you need to keep the path from the shortcut to the program intact. But, other than that, you can put the joined shortcut and program everywhere you want – it will keep on working.

Nice?

So, here is my small contribution to the world: you CAN create shortcuts to programs with relative path, with just a minor tweak!

PS: If you wish to comment, don’t forget to put the number twelve in the box that asks «Απάντηση»

10 thoughts on “You CAN create shortcuts to programs with relative path for your USB!

  1. (Since the post is in English, I ‘ll comment in English too)

    You must be getting something wrong here. %path% variable holds paths to executables, so that when you only give the OS an executable filename to run, if the OS doesn’t find it in the current directory, it goes looking for it at the end of each path in the %path% variable value, until it finds and executes it, or not and responds with a not-found message.

    You can achieve what you are after, i.e., have a «shortcut» to applications that reside on your removable drive, on the removable drive itself, by creating a batch file instead of a shortcut links. In batch files, your are allowed to start an absolute path from the root of the current drive, so just put the whole path from root to the app, followed by backslash and the app filename and you are done; no drive names needed.

  2. Does it do the job for you? If it does, its a catch

    Of course it doesn’t!

    As you already should know, testing the validity of some piece of software by random, or even convenient, input cases, does not suffice. You need to prove that it is valid for any input.

  3. Well,

    Its good to know that people might be using this 😉
    So I can create a program-trojan horse, that installs itself in the very first folder specified in %Path% (C:\Windows\System32\; usually) and name it for example…. firefox.exe 😉
    This program could first copy your password file to a specific server 😉 and then load the proper Firefox.exe…

    So I suppose its quite common for people to run firefox from USBs…
    Now guess, which Version of Firefox.exe will be executed if you insert your Usb and type %path%\Program File\firefox.exe
    😉

  4. NO! It does not work. I tried on 2 computers and 3 laptops. It may work for him because his PATH variable is modified and contains all the drives letters (many at least), or it has «.» in it, or %CD% in it, or whatever else.
    But it could and will not work for other users that do not have that modified, so it cannot be standardized for other to use as a way do do what it think he is accomplishing.
    There are other ways, that really works like:
    (ordered in increasing complexity)

    the simplest: create a batch file containing

    @echo off
    cd .\yourdir\yourprgramdir
    start yourprogram.exe

    (and you can build on this one to make more custom like:

    @echo off
    rem Start YOURPROGRAM using the appropriate interpreter and environment variable
    setlocal EnableDelayedExpansion
    set CURRDIR=%~dp0
    set YOURPROGRAMHOME=%CURRDIR%..\..\whatevermore
    set YOURPROGRAMPATH=%YOURPROGRAMHOME%..\Sources\etc.etc.
    PATH or WHATEVERVARIABLEPATHYOURAREUSING=%YOURPROGRAMHOME%;%YOURPROGRAMPATH%;%CURRDIRR%..\Sources\etc.etc.
    cd %YOURPROGRAMPATH%
    start «YOURPROGRAMINTERPRETERorCOMPILERor..» «%YOURPROGRAMHOME%\program.exe» «%CURRDIR%tobeexecuteorcompiled.code» %1 %2 %3 %4 %5 %6 %7 %8 %9

    or read here
    http://www.csparks.com/Relative/index.html

    or
    http://answers.microsoft.com/en-us/windows/forum/windows_vista-files/how-to-create-a-shortcut-with-relative-path/3e1b0ede-1e18-4ecd-937b-66756d3409d3
    (and you could modify the way explained in the above link by using the %CD% environmental variable for the OS that support it)

    or
    http://www.freewaregenius.com/how-to-create-shortcuts-with-a-relative-path-for-use-on-usb-drives/

    or
    http://www.howtogeek.com/109788/how-to-create-shortcuts-to-programs-on-usb-drives/

    or
    Even create your on C/C++ or even Assembly code.

    P.S.: The variable PATH in Windows environment comes from the DOS days and is supported by CMD.exe and Command.com and works accordingly.
    Before DOS era PATH came from CP/M Systems and UNIX style systems even before that.

    Cheers

Αφήστε μια απάντηση

Η ηλ. διεύθυνσή σας δεν δημοσιεύεται.