82

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.

8 Answers8

75

For relative to shortcuts to folder or files (but not with arguments), you can use the utility Relative.

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

You can also 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
  • 13,565
49

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.

16

This trick works :

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

example

%COMSPEC% /C start winmine.exe

9

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
  • 191
8

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

For anyone wanting to do this as part of a powershell script it can be accomplished with the following.

$WshShell = New-Object -comObject WScript.Shell
$Shortcut = $WshShell.CreateShortcut("path\to\shortcut.lnk")
$Shortcut.TargetPath = "%windir%\system32\cmd.exe"
$Shortcut.Arguments = "/c start .\relative\path\to\binary.exe"
$Shortcut.WorkingDirectory = "%CD%"
$Shortcut.Save()
3

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
  • 31
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

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.

fixer1234
  • 28,064
gmoises
  • 19
  • 3