Using relative paths for Windows shortcuts

53

29

I have a folder scheme like (highly simplified version):

New Files
 >Tools
 >Scripts
Tested Files
 >Tools
 >Scripts

... and I'd like to have a shortcut in each folder from the "New Files" child folders, to the "Tested Files" child folder. But this folder may be moved around from time to time, which would break said shortcuts.

Is there a way to make a relative shortcut to each folder? I remember doing this in HTML where you could set a path, something along the lines of .../Files to go back to a parent and then into a new folder, but I'm unsure if this is something support under Windows shortcuts?

PS: The case of similarly relative shortcuts, when the target is a file, is dealt with in https://stackoverflow.com/questions/1169556/making-a-windows-shortcut-start-relative-to-where-the-folder-is. In the present case the target is a Folder.

Gary Morris

Posted 2013-09-12T14:49:06.043

Reputation: 641

https://stackoverflow.com/q/1169556/2707864 – sancho.s Reinstate Monica – 2017-10-20T10:30:35.290

@sancho.s - AFAIK, the answers there only work for executing .bat files. What's needed here is a way to refer to a folder instead. – ToolmakerSteve – 2017-12-18T21:19:31.880

@ToolmakerSteve - I guess you checked / read "the answers there" only superficially. One of them (https://stackoverflow.com/a/1751350/2707864) is the accepted solution here, that one being ca. 4 years older. I did not try it. Another one (https://stackoverflow.com/a/29261618/2707864) is the second half of the accepted solution here. You even commented on that one. I tried it and it works. It is not necessary to be thorough to place a comment, but one should try not to mislead readers.

– sancho.s Reinstate Monica – 2017-12-19T10:17:45.580

@sancho.s - Actually, I read those answers in detail. However, I misunderstood Relative when I followed the link to its description. [this was before I read Rik's answer] I guess I was confused by the fact that those answers all boiled down to using features to execute code (since that Q was about executing). You providing the link to that set of answers, with no further clarification, increased my confusion, rather than leading me closer to a solution. Now that you have specified what in all those answers was relevant, it is helpful. Thank you. – ToolmakerSteve – 2017-12-22T01:23:47.147

@ToolmakerSteve - As I said, a comment does not need to be thorough, but it would avoid misleading. My only posting the link means check this, it might be useful; I simply found it, but did not dig any further (as usual in SO). A warning (even with AFAIK) that "the answers there only work for executing .bat files" may be misleading, when the case is that the answers there were only meant for executing .bat files, though they might work. – sancho.s Reinstate Monica – 2017-12-22T08:53:30.983

i have the same problem as this, but we need additional feature on the solution. the shortcut file with relative path should not open a new explorer window. it should use the same explorer window like the default behavior of windows shortcut file when full path is used instead of relative path. – acegs – 2019-10-26T01:42:42.833

1Did you try ../Files, with two dots? I'm not running Windows but I think it may work. – Brian Z – 2013-09-12T14:53:33.920

1That's the first thing i tried, thanks though. :( – Gary Morris – 2013-09-12T15:12:13.033

Answers

53

You can use this utility: Relative.

It basically creates a shortcut to "explorer.exe" with the parameter of your relative path with a right click (same way as you create a normal shortcut).

Of course you can do this manually.
In your example you would create a shortcut in "New Files\Tools" to

%windir%\explorer.exe "..\..\Tested Files\Tools"

You can use the usual context-menu "New/Create shortcut" of Windows for this and typing above command in "Type the location of the item"-box.

Rik

Posted 2013-09-12T14:49:06.043

Reputation: 11 800

2The manual solution here does not work for me out of the box. In addition, I have to change "Start In" from %windir% to empty. – notan3xit – 2017-12-01T08:48:37.350

In Windows 10, to get to a subfolder from my main folder, I had to use a variation on the manual solution. I changed "Start In" to "%CD%". Then %windir%\explorer.exe "Tested Files" got me to that subfolder. – ToolmakerSteve – 2017-12-18T22:18:45.410

1is there a solution to use the same explorer window where the shortcut file is opened? using this solution opens a new explorer window. i currently need a shortcut file with relative path that when opened, it still uses the same explorer window. – acegs – 2019-10-26T01:39:41.390

@acegs Unfortunately this is not possible with running/starting explorer.exe. It can't connect to a previous version. Using cmd /c "start ..\directory" could do this but as soon as you change the directory, it doesn't get recognized as a process to be used for a new explorer.exe. – Rik – 2019-11-15T11:19:57.013

I did read about that during my previous research, but i was hoping for a native solution within Explorer. This will be my backup plan though, cheers. – Gary Morris – 2013-09-12T15:13:24.523

By the way, will this relative path stay the same between systems also? Or will i need Relative to be installed if i move the folders/contents between different systems? – Gary Morris – 2013-09-12T15:16:25.693

3"Relative" only makes the shortcut using the standard "%windir%\explorer.exe" so you don't need Relative on the different system. It would be the same as typing %windir%\explorer.exe before the relative path while making a standard shortcut. So if you do this manually you won't need Relative at all. (You just need to remember the command before your relative path) – Rik – 2013-09-12T16:16:23.857

Ah sorry, i misunderstood that part of your first reply, my bad! Thanks so much! – Gary Morris – 2013-09-12T17:00:01.233

31

One possible solution is use a one line batch file instead of a short cut to open whatever you wanted to open. The batch file will let you use relative paths inside itself and will have a working directory of whatever folder the batch file is placed in.


Another option is have your shortcut start cmd.exe instead with whatever you are launching then pass whatever it is you are launching in as a argument to cmd.exe

enter image description here

%COMSPEC% is a environment variable points to the command prompt by default.

/C causes the console to close itself after it executes the command.

Scott Chamberlain

Posted 2013-09-12T14:49:06.043

Reputation: 28 923

Thank you very much! This worked for me. I spent hours trying to get my shortcut execute a power-shell script in the same directory. – Shervin Shahrdar – 2017-03-13T17:11:29.490

1Useful for a different situation, however the question isn't about opening some file; it is about being able to jump to a specified folder within Windows Explorer. Is there a way to adapt this answer to do so? – ToolmakerSteve – 2017-12-18T21:22:17.890

7

I'm using similar solution in a template that runs my web development environment (open project directory, open browser, run WAMP, run SCSS...)

enter image description here

I can pass arguments to my bat script and etc., this is cool. Make sure to put /c argument after cmd.exe

Hrvoje Golcic

Posted 2013-09-12T14:49:06.043

Reputation: 171

6

This trick works :

%COMSPEC% /C start "your exe name without path"

example

%COMSPEC% /C start winmine.exe

Alexis PERROTTEY

Posted 2013-09-12T14:49:06.043

Reputation: 87

3Please read the question again carefully. Your answer does not answer the original question. – DavidPostill – 2016-08-18T17:37:45.477

2For Win10 1607, this solution answers the OP's question perfectly,

Target: %COMSPEC% /C "start GoogleChromePortable.exe -enableextensions -incognito" Start In: [leave blank] – semtex41 – 2017-02-04T05:42:27.127

@semtex41 Huh? I try that, and the result is an error dialog saying "Windows cannot find 'GoogleChromePortable'. What does that have to do with OP's goal of navigating Windows Explorer to a specified folder? – ToolmakerSteve – 2017-12-18T21:27:45.933

@ToolmakerSteve well the title is "Using relative paths for Windows shortcuts" and I was contributing to a previously supplied answer. And the OP's goal is actually to make the paths dynamic, vs static. So since my reply is how I created a shortcut that executes in a non-static path, I believe my answer fits. – semtex41 – 2017-12-18T21:37:13.470

1@semtex41- OP's stated goal is ".. a shortcut .. to the "Tested Files" child folder". If you aren't explaining how to have this answer accomplish that, then please clarify what you were adding to the answer. It looks like you just gave another example of how to execute a .exe file. But the answer already showed such an example. So please re-state the point you were making? – ToolmakerSteve – 2017-12-18T21:55:00.947

@ToolmakerSteve. if you think you are helping this community by trolling, you are wrong. My answer has at least +1up, so it helped someone. It contributes 1) by confirming functionality 2) mentions to leave the "Start In" blank. Have a Merry Christmas. Move on. – semtex41 – 2017-12-18T22:15:59.470

Your answer aims to the case where the target is an executable file. The OP is interested in shortcuts to folders. Please check if your answer addresses the case, and post an explicit example for that. – sancho.s Reinstate Monica – 2017-12-19T10:10:47.560

I found this solution to be the most efficient and practical, thanks.

@sancho.s I just tested this on Win. 10, it also works on shortcuts pointing to folders. So this answer did in fact fully answer the OP's question. – plu – 2019-08-22T00:26:31.047

It seems to lack a pair of empty quotes, though: %COMSPEC% /C start "..\..\PortableApps\Test.exe" wouldn't work for me (just opens a cmd at the given path).%COMSPEC% /C start "" "..\..\PortableApps\Test.exe" works fine. – Marcus Mangelsdorf – 2020-02-13T12:59:20.070

4

You can use mklink. It allows you to create symbolic links, hard links and directory links.

 mklink /d Tools "..\Tested Files\Tools"  (elevated command prompt)

If there is no elevated access, you can use /j

 mklink /j Tools "..\Tested Files\Tools"

To move around the whole structure you should use the xcopy command. For example, if all the structure is under container:

container
   New Files
      <SYMLINKD> Scripts [..\Tested Files\Scripts]
      <SYMLINKD> Tools  [ ..\Tested Files\Tools]
   Tested Files    
      Scripts
      Tools

entering the command

 xcopy /b /e container container2

will create the following structure:

container2
   New Files
      <SYMLINKD> Scripts [..\Tested Files\Scripts]
      <SYMLINKD> Tools  [ ..\Tested Files\Tools]
   Tested Files    
      Scripts
      Tools

The /b switch will copy the Symbolic links instead of converting them to folders. (Note that /b has a completely different meaning for the copy command)

Krauss

Posted 2013-09-12T14:49:06.043

Reputation: 141

But will that link still be correct, if the set of folders is moved to a different location? – ToolmakerSteve – 2017-12-18T21:29:22.133

This requires privileges I do not have, although it looks like it would do the job... Would you mind posting an image of the resulting Shortcut properties, once it was created? – sancho.s Reinstate Monica – 2017-12-19T10:28:29.247

1The links are indeed correct. That can be confirmed with the "dir" command. The problem is that "copy-paste", "move" and "copy" actions of the File Manager will destroy the structure. I have modified the answer to include a workaround. – Krauss – 2018-09-14T18:53:53.100

1

If you leave the 'Start In' box empty in the properties of the Shortcut, the links be relative to the current working directory.

See also https://stackoverflow.com/a/17951772/40961

David d C e Freitas

Posted 2013-09-12T14:49:06.043

Reputation: 3 498

How does this help OP's goal of navigating Windows Explorer to a specified folder? – ToolmakerSteve – 2017-12-18T21:31:41.090

If you had the problem then the solution makes sense... how does your comment help on every answer? – David d C e Freitas – 2018-01-13T12:51:05.213

1

A shortcut can record it's location in a variable and call a command using the variable. For example, create the shortcut "Grandparent" with target:

%windir%\system32\cmd.exe /c set HERE="%CD%" && "C:\Here.bat"

Create the batch file "C:\Here.bat" with the single line:

@%windir%\explorer.exe /n,/select, %HERE%

Now, whatever folder Grandparent is in, when you click it, the parent of its parent folder opens. It even works with Grandparent in a root directory.

Your batch file could have used %HERE% in starting something other than explorer.exe. Or instead of Here.bat after the && in the shortcut target, you could call a program that makes use of %HERE%.

On my system Grandparent seems to work with & or &&.

mudr

Posted 2013-09-12T14:49:06.043

Reputation: 11

1&& only performs the next call if the previous call did not return an error, while & doesn't care. In this case, there should be no difference. – leewz – 2016-06-08T02:02:47.453

0

You can create an environment variable that contains the (relative) Path to the target folder or a folder above it in the file system structure.

Example:

  • Environment Variable:

    %Dropbox% = "C:\Users\User 1\Dropbox"

  • Shortcut Target:

    "%Dropbox%\Install\Utilities\File.exe"

You can use the DOS command SETX to create environment variables.

gmoises

Posted 2013-09-12T14:49:06.043

Reputation: 9