70

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?

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
William
  • 8,162
  • 23
  • 73
  • 106
  • If somebody else installs the game, will that affect the existing shortcut? Will they be installing to a completely different folder, but it will still break the existing link? – SqlRyan Jul 23 '09 at 04:10

15 Answers15

74
  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.

jpaugh
  • 5,719
  • 4
  • 33
  • 83
leoj
  • 1,199
  • 9
  • 20
  • 2
    Does anyone know how to force `cmd.exe /c start` to use the currently open *Windows Explorer* window? – leoj Nov 17 '11 at 08:06
  • 1
    Another note, since I just dealt with it (on XP at least): ensure the "Start in:" field is empty. – chezy525 Oct 15 '12 at 21:00
  • 2
    Note that this won't work if the shortcut is set to run as an Administrator: http://stackoverflow.com/questions/18756671/is-it-possible-to-make-a-shortcut-to-a-relative-path-in-windows-that-runs-as-adm – Sphinxxx Jan 25 '15 at 01:33
  • 2
    What if we want a shortcut to a data file? (`.txt`, `.ini`, `.md`) – Stevoisiak Mar 06 '17 at 20:19
  • New challenge: I want a relative shortcut to a folder, but it should open in the same window. I guess that's impossible. – Fabian Röling Oct 16 '17 at 21:42
  • 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://stackoverflow.com/a/13811519/6483483 . Sadly I do not have a solution to this issue. – blackandorangecat Oct 20 '17 at 23:50
  • @StevenVascellaro See https://superuser.com/a/644421/187095 Also see my comment on that answer. – ToolmakerSteve Dec 18 '17 at 22:20
  • Works also with Windows 10 – habakuk Feb 21 '20 at 14:46
38

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

David d C e Freitas
  • 7,146
  • 4
  • 54
  • 66
Bob Pollack
  • 499
  • 4
  • 2
7

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.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
TimDC
  • 123
  • 4
  • 10
  • 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 ) so – Kamlesh Nov 19 '12 at 08:48
  • @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. – bers Feb 13 '18 at 08:11
  • 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. – bers Feb 13 '18 at 08:12
4

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.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
SqlRyan
  • 30,939
  • 32
  • 109
  • 190
  • 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. – SqlRyan Jul 23 '09 at 04:21
3

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.

ToolmakerSteve
  • 5,893
  • 8
  • 67
  • 145
darda
  • 2,267
  • 5
  • 29
  • 45
3

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"

George Green
  • 4,577
  • 5
  • 28
  • 45
Charles P.
  • 1,479
  • 12
  • 13
  • 1
    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.bat – GlennFromIowa Dec 01 '16 at 19:24
  • 1
    Does WIndows support `.`? `%windir%\explorer.exe ".\data\run.bat"` - will that work? – ToolmakerSteve Dec 18 '17 at 21:03
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.

Alexis B.
  • 167
  • 1
  • 1
  • 9
  • 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? – ToolmakerSteve Dec 18 '17 at 21:07
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

Community
  • 1
  • 1
John Thow
  • 361
  • 1
  • 3
  • 11
  • Can you really use %CD% in the "Start In" box? This has proven to be troublesome. Here are some alternatives: https://serverfault.com/a/863325/184613 – JonathanDavidArndt Feb 27 '18 at 18:08
1

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.

Mehrdad Mirreza
  • 794
  • 8
  • 20
1

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.
zar
  • 9,241
  • 10
  • 74
  • 145
1

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

0

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

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Vincent De Smet
  • 4,434
  • 1
  • 30
  • 40
  • 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) – Anders Jul 24 '09 at 12:23
  • 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://stackoverflow.com/a/29261618/199364 Based on Anders comment, there is some way to refer to "subdirectory data of current directory"? – ToolmakerSteve Dec 18 '17 at 21:14
0

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.

LokizFenrir
  • 356
  • 3
  • 9
0

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.

0

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
Garric
  • 195
  • 1
  • 5