Modifying Windows Shortcut .lnk-file's target into a relative path

  • user13267

    Copied from here, as it's locked over there; I hope it belongs here.

    Is it possible to open .lnk (windows shortcut) in a hex editor, and change the absolute shortcut path into a relative one? Can we do this in Windows? The edit command in cmd can open link files but it's difficult to read and edit. Is there any hex editor in windows that can open an .lnk file and allow me to edit it? Do I need to take it to Linux or does Linux recognize .lnk as a shortcut too?

    I want to do this not only for running .exe files, but also for pointing to folders (that is, shortcut to folders). A batch file which executes explorer.exe with the target folder as parameter can do this actually, but I want to know if there is any way to actually edit the data in the .lnk file itself

  • Answers
  • Karan

    Is there any hex editor in windows that can open lnk file and allow me to edit it?

    I don't see why this should be a problem for any hex editor. It might edit the source if you create a symlink or something, but unless the editor specifically has a feature to transparently resolve shortcuts and edit the source, it should work just fine for editing the .LNK itself. I just tried editing both file and dir shortcuts using HxD and had no issues. Of course, if you're looking for a hex editor that actually parses the file and allows you to modify it, you might be out of luck.

    There are also command-line programs that you can use to create and modify various aspects of shortcuts, as also Visual Basic scripts.

    However, all this is pointless because Windows does not support relative shortcuts in the first place, as can be seen from this Raymond Chen post (be sure to read the associated comments as well).

  • Related Question

    command line - Can I create a shortcut to a Windows directory?
  • jmgant

    I would like a quick command line-based way to get to a directory I use all the time. Is there a way to create some kind of alias in Windows so that I can type, for example, VS08P at a command prompt or in the address bar and Windows will automatically open the folder I want, which is c:\Documents and Settings\[My ID]\My Documents\Visual Studio 2008\Projects?

    (Note that the question isn't about what to do with Visual Studio 2008. Unless the solution is a batch file, I think I've asked the question on the right site.)

  • Related Answers
  • Colin Pickard

    If you want to open that folder in Windows Explorer, you can either:

    1. Create a shortcut (.lnk file) to a folder (by right clicking > New > Shortcut in Windows Explorer or on your desktop) then drop that shortcut somewhere in your path.
    2. Create a batch file like this:

      cd "c:\Documents and Settings\etc\etc"
      start .

      then save it as VS08P.bat and put it somewhere in your path.

    If you want to jump to it in your command prompt, see Phoshi's answer.

  • akf

    I will add to the 'create a simple batch' cacophony, with a twist. You can create a simple batch, but put a switch in side, such that you can use it to navigate to variety of favorite dirs:

    @echo off
    GOTO %1
    cd c:\Documents and Settings\[My ID]\My Documents\Visual Studio 2008\Projects
    cd "C:\Documents and Settings\[My ID]\My Documents\My Music"
    cd C:\shared\downloads
    cd C:\[project path]\logs

    You can call it go.bat, and you can use it for all your favorite locations Your friends will think you are really cool because you can just type go logs on the commandline and you are magically taken to your logs directory. You will still need to append the dir within which this bat is saved to your PATH.

  • Oliver Salzburg

    You could set a custom environment variable:

    • Right-click "My Computer" and select "Properties"
    • Select the tab "Advanced"
    • Click the button "Environment variables"
    • At the top part of the window you can define your own system variables

    I have a German Windows installation so the names of the items mentioned above can be slightly different ;)

    Another way would be:

    Create a batch with the following content and run the shell through this:

    @echo off
    set VS08P = c:\Documents and Settings\[My ID]\My Documents\Visual Studio 2008\Projects

    This way you can add as many vars as you like and call them inside your shell session.

  • Phoshi

    The solution is probably a batch file. Make a VS08P.bat in system32 (normally :\windows\system32) that contains:

    cd c:\Documents and Settings\[Your ID]\My Documents\Visual Studio 2008\Projects\

    Then, typing VS08P should take you there.

  • Brad Patton

    Use the mklink command. From the command-line:

    C:> mklink /D VS08P c:\Documents and Settings\[My ID]\My Documents\Visual Studio 2008\Projects

    You will now have c:\VS08P that points to your Visual Studio directory above.

  • Milde
    set myDir=C:\Windows
    cd %MyDir%

    This works in the current command-line.

    To make this static set an environment variable under "Start->Settings->Controls->System->Advanced->Environment variables"

    Set a Name and a Path (e.g. mypermaDir - C:\Windows)

    Now, you can use this new variable:

    cd %mypermaDir%
  • caliban

    how about a one-line batch file (to be placed in the standard or customized "Environment Variables" :

    explorer.exe "c:\Documents and Settings[My ID]\My Documents\Visual Studio 2008\Projects"

    various switches you can use too after explorer.exe

    /e - no tree pane

    /n - use new Explorer window

  • Seasoned Advice (cooking)

    How about this simple command line to be placed in a batch file :

    %windir%\explorer.exe %userprofile%\My Documents\Visual Studio 2008\Projects

    Using environment variables is a good way to have a portable code.