Making a Windows shortcut start relative to where the folder is?
Asked Answered
M

17

83

I have a game that uses this file structure:

GAME FOLDER
->data
->data->run.bat

I want to put a shortcut to run.bat in GAME FOLDER, but if I move it, or someone else installs it it won't work, because the target is wrong. Is there a way to make the target and "start in" relative to GAME FOLDER?

Mcintire answered 23/7, 2009 at 4:6 Comment(0)
F
85
  1. Right click on your /bat/ folder and click Create Shortcut.

    • On Windows 7 you will get bat - Shortcut in the current directory.
    • On Windows XP you will get Shortcut to bat.
  2. Right click on the shortcut you just created and click Properties.

  3. Change Target (under the Shortcut tab on Windows 7) to the following:

    %windir%\system32\cmd.exe /c start "" "%CD%\bat\bat\run.bat"
    
  4. Make sure Start in is blank. That causes it to start in the current directory.

  5. Click OK. On Windows 7, the shortcut icon will change to the cmd.exe icon.
  6. That's probably acceptable in the case of shortcutting to a .bat but if you want to change the icon, open the shortcut's properties again and click Change Icon... (again, under the Shortcut tab on Windows 7). At this point you can Browse... for an icon or bring up a list of default system icons by entering

    %SystemRoot%\system32\SHELL32.dll
    

    to the left of the Browse... button and hitting Enter. This works on Windows 7 and Windows XP but the icons are different due to style updates (but are recognizably similar). Depending on the version of Windows the shortcut resides, the icon will will sometimes change accordingly.

More Info:

See Using the "start" command with parameters passed to the started program to better understand the empty double-quotes at the beginning of the first Target command.

Francis answered 17/11, 2011 at 7:58 Comment(10)
Does anyone know how to force cmd.exe /c start to use the currently open Windows Explorer window?Francis
Another note, since I just dealt with it (on XP at least): ensure the "Start in:" field is empty.Auklet
Note that this won't work if the shortcut is set to run as an Administrator: #18757171Ganda
What if we want a shortcut to a data file? (.txt, .ini, .md)Cosy
New challenge: I want a relative shortcut to a folder, but it should open in the same window. I guess that's impossible.Mileage
This solution works if you want to run the .bat file as regular user, but does not work if you tell the shortcut to run as an administrator as instructed in this question: https://mcmap.net/q/134262/-how-to-code-a-bat-file-to-always-run-as-admin-mode . Sadly I do not have a solution to this issue.Tresatrescha
@StevenVascellaro See superuser.com/a/644421/187095 Also see my comment on that answer.Hogweed
Works also with Windows 10Involute
For shortcut in Windows 11, I did this to start window in "C:\Users\" (no bat file needed): %windir%\system32\cmd.exe /k "cd C:\Users\Aurignacian
make that: %windir%\system32\cmd.exe /k "cd C:\Users\"Aurignacian
G
47

According to Microsoft, if you leave the 'Start In' box empty, the script will run in the current working directory. I've tried this in Windows 7 and it appears to work just fine.

Source: http://support.microsoft.com/kb/283065

Gaffrigged answered 30/7, 2013 at 15:54 Comment(7)
Is there a way to get Explorer to browse to the relative path without opening a new window?Swagman
And if you want to make it explicit, you could use Start In = %CD%Emulsoid
Great, empty, or %CD% also works in Windows 10. Such elegant and simple solution!Janik
Empty, or %CD% works in the native Windows 10 explorer, but be warned it may not work in other file explorers. I'm having trouble in Adobe Bridge which likes to choose it's own working directory unrelated to the one you're working in.Homager
What is the "current working directory" for a shortcut? The directory in which the shortcut file is located? The directory from which File Explorer was started?Spitler
The link in the answer text is broken.Spitler
It does not work on windows 10. It says target box is not validGarretgarreth
V
6

If you can set a system variable (something like %MyGameFolder%), then you can use that in your paths and shortcuts, and Windows will fill in rest of the path for you (that is, %MyGameFolder%\data\MyGame.exe).

Here is a small primer. You can either set this value via a batch file, or you can probably set it programmatically if you share how you're planning to create your shortcut.

Volpe answered 23/7, 2009 at 4:13 Comment(1)
For example, my shortcut for Notepad points to "%SystemRoot%\system32\notepad.exe", which automatically resolves whatever the equivalent of C:\Windows is on your computer.Volpe
S
6

Try using Relative (a Windows command-line application).

Basically, a shortcut could have a relative link, but Windows gives no way to actually make one.

Sanctus answered 17/11, 2009 at 19:57 Comment(3)
i use this way but when i delete shortcut file and then delete original file also . but this is way is not proper( Do not delete original file when delete shortcut file ) soSelfpropulsion
@kamlesh0606 I see no reason why deleting the relative shortcut created by Relative should delete the original fine (I have tried it). In fact, the shortcut does not even reference the "original file" direct, it targets "%windir%\explorer.exe". Maybe, by mistake, you used the "mklink" command which is also explained on the Relative download page and created a hard link? That could explain what you are seeing.Hanks
This works, but it is basically a shortcut to "%windir%\explorer.exe", it is not a real relative link: e.g., the icon is different from that of the target file.Hanks
G
4

You can make a relative shortcut manually by changing the file path. First in the usual context-menu you create a new shortcut of Windows for your file and in the properties -> location of your file:

%windir%\explorer.exe "..\data\run.bat"

Gillett answered 25/3, 2015 at 16:46 Comment(2)
Um, actually, that would run run.bat in a data folder that is the same level as GAME FOLDER. For example, if the batch file is C:\Games\GAME FOLDER\data\run.bat, putting that shortcut in GAME FOLDER would look for C:\Games\data\run.batDreadfully
Does WIndows support .? %windir%\explorer.exe ".\data\run.bat" - will that work?Hogweed
Y
2

I like leoj3n's solution. It can also be used to set a relative "start in" directory, which is what I needed by using start's /D parameter. Without /c or /k in as an argument to cmd, the subsequent start command doesn't run. /c will close the shell immediately after running the command and /k will keep it open (even after the command is done). So if whatever you're running spits to standard out and you need to see it, use /k.

Unfortunately, according to the lnk file specification, the icon is not saved in the shortcut, but rather "encoded using environment variables, which makes it possible to find the icon across machines where the locations vary but are expressed using environment variables." So it's likely that if paths are changing and you're trying to take the icon from the executable you're pointing to, it won't transfer correctly.

Yvor answered 26/1, 2013 at 20:29 Comment(0)
S
2

After reading several answers, I decided to do it with a simple solution: Instead of a shortcut, I made a .bat with only one line to call the main .bat and it works like I wanted.

Stamps answered 5/7, 2016 at 15:56 Comment(1)
How does this help? That is, if someone copies all of this to a different folder, how does your .bat find the main .bat in the new location?Hogweed
P
2

I'm not sure if I'm right, or I'm missing something, but as for now (2016-07-11, running Win7 Enterprise SP1) a LNK file adapts itself on moving or even changing the drive letter after it is run at a new place! I created a new shortcut on my USB drive and tried moving the shortcut and its target in a way that the relative position stayed unchanged, then I changed the drive letter. The shortcut worked in both cases and the target field was adapted after I double-clicked it.

It looks like Microsoft has addressed this issue in one of the past updates.

Please somebody confirm this.

Platte answered 11/7, 2016 at 14:17 Comment(0)
R
2

After making the shortcut as you have, set the following in Properties:

Target: %comspec% /k "data\run.bat"

  • Drop the /k if you don't want the prompt to stay open after you've run it.

Start In: %cd%\data

Riba answered 1/8, 2016 at 9:34 Comment(1)
Can you really use %CD% in the "Start In" box? This has proven to be troublesome. Here are some alternatives: serverfault.com/a/863325/184613Gamo
M
2

I tried %~dp0 in the Start in field and it is working fine in Windows 10 x64

Merthiolate answered 30/1, 2020 at 4:11 Comment(0)
R
1

You could have the batch file change the current working directory (CD).

Ruderal answered 23/7, 2009 at 4:21 Comment(2)
This is what I would recommend also. On NT5+ at least, you can get the path to the currently running batch file with %~dp0 (If command processor extensions are on, and the are by default, but you should call setlocal at the start of your batch to make sure)Duckpin
This needs to be explained in more detail. For this to still work after the files are moved elsewhere, the batch file must contain a relative path. Please show what the cd command would look like, with a relative path to the subfolder data. Note that ..\data wouldn't work, it is one parent too high, as discussed in Glenn's comment on https://mcmap.net/q/182571/-making-a-windows-shortcut-start-relative-to-where-the-folder-is Based on Anders comment, there is some way to refer to "subdirectory data of current directory"?Hogweed
E
0

The link with a relative path can be created using the mklink command on windows command line.

mklink /d \MyDocs \Users\User1\Documents

This might be the best way to create link because apparently, the behaviour of shortcut can be different perhaps based on the way they are created (UI vs mklink command). I observed some strange behavior with how the shortcuts behave when I change the root folder.

  • There is a weird behaviour on Windows 7 that I tested. Sometimes the the link still just works when the root folder of target is changed (the shortcut properties automatically update to reflect the changed path!). The "start in" field updates automatically as well if it was there.
  • I also noticed that one link doesn't work the first time I change the root path (properties shows old) but it works after the 2nd and everytime after that. The link properties get updated as result of the first run!
  • I also noticed at least for two link, it doesn't update the path and no longer works.
  • From link properties, there is no difference in format of any fields yet the behaviour is different.
Elatia answered 17/5, 2018 at 19:29 Comment(0)
R
0

I have tried the many methods you described but somehow not worked in my W11 machine. So I have used a python library to create shortcut(s) automatically. If you want to use python to create shortcut please do following:

If pywin32 not installed, you can install it using pip:

pip install pywin32

Then, please open a python file and write following:

import os
import win32com.client

# Get the absolute path of the target file
target_path = os.path.abspath(YOUR_EXE_PATH)

# Define the location and name of the shortcut
shortcut_path = os.path.join(os.path.abspath(YOUR_SHORTCUT_PATH), 'shortcut.lnk')

# Create a shortcut
shell = win32com.client.Dispatch("WScript.Shell")
shortcut = shell.CreateShortCut(shortcut_path)
shortcut.Targetpath = target_path
shortcut.WindowStyle = 1 # Normal
shortcut.save()

With this method, I created shortcuts automatically..

Ramiah answered 21/3 at 9:49 Comment(0)
T
-1

Easiest Solution:> Enviroment Variables handy little critters.

If the other person is to install/uncompress whatever to wherever on their respective system drive (usualy c:).

For demonstration purposes call our app "test.exe" (could be any executable/file doesn't have to be exe) and it is to be installed/uncompressed to folder MYCOMPANY\MYAPP\

Then just make a shortcut that uses %SystemDrive%\MYCOMPANY\MYAPP\test.exe as target and %SystemDrive%\MYCOMPANY\MYAPP\ as start in.

So now you would like to deploy it. Using an app like "WinRAR".

Easy way is using self-extraction zip file, neatly packaged as an ".exe" I would use one for my shortcut and another for the app. There is ways to make one self-extraction zip file that extracts different files to different directories, but i haven't played with it yet.

Another way is to make a selfextract for the shorcut, embed it inside the selfextract for the app and then apply a run once script,being that you know where the file is going to be. etc.

If you want to enable the installer to use custom installation/uncompress directories, then rather have a look at NSIS a scriptable install system.

Play around it's fun, hope my info helped.

Tame answered 23/5, 2013 at 21:18 Comment(1)
This doesn't seem to address the simple need for a shortcut that works relative to the shortcut folder.Aileneaileron
W
-1

Just a small improvement to leoj3n's solution (to make the console window disappear): instead of putting %windir%\system32\cmd.exe /c start "" "%CD%\bat\bat\run.bat" to the Target: field of your Windows shortcut, you can also try adding only the following: %windir%\system32\cmd.exe /c "%CD%\bat\bat\run.bat" AND then also adding start in front of your commands in run.bat. That will make the console window disappear, but everything else remains the same.

Wieland answered 27/9, 2015 at 19:27 Comment(0)
C
-1

The method that proposed by 'leoj' does not allow passing parameters with spaces. Us it:

    cmd.exe /v /c %CD:~0,2%"%CD:~2%\bat\bat\run.bat" "Par1-1 Par1-2" Par2

Which will be similar double quote written as in path

    C:"\Program Files\anyProgram.exe" "Par1-1 Par1-2" Par2
Cross answered 31/5, 2020 at 18:22 Comment(0)
D
-2

Make a symbolic link of a relative path. On the command prompt cmd (with Administrator privileges):

mklink /D SYMLINK_NAME RELATIVE_TARGET_PATH
Disgust answered 18/12, 2021 at 21:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.