使 Windows 快捷方式相对于文件夹所在的位置开始?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1169556/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 06:35:38  来源:igfitidea点击:

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

windowsshortcuts

提问by William

I have a game that uses this file structure:

我有一个使用此文件结构的游戏:

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

I want to put a shortcut to run.batin 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?

我想run.bat在 GAME FOLDER 中放置一个快捷方式,但是如果我移动它,或者其他人安装它,它将不起作用,因为目标是错误的。有没有办法使目标和“开始”相对于游戏文件夹?

回答by leoj

  1. Right click on your /bat/ folder and click Create Shortcut.

    • On Windows 7 you will get bat - Shortcutin 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 Shortcuttab on Windows 7) to the following:

    %windir%\system32\cmd.exe /c start "" "%CD%\bat\bat\run.bat"
    
  4. Make sure Start inis 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 Shortcuttab 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 willwill sometimeschange accordingly.

  1. 右键单击您的 /bat/ 文件夹,然后单击Create Shortcut

    • 在 Windows 7 上,您将进入bat - Shortcut当前目录。
    • 在 Windows XP 上,您将获得Shortcut to bat.
  2. 右键单击您刚刚创建的快捷方式,然后单击Properties

  3. 目标(在Windows 7 上的快捷方式选项卡下)更改为以下内容:

    %windir%\system32\cmd.exe /c start "" "%CD%\bat\bat\run.bat"
    
  4. 确保Start in为空白。这会导致它在当前目录中启动。

  5. 单击确定。在 Windows 7 上,快捷方式图标将更改为 cmd.exe 图标。
  6. 在快捷方式到 .bat 的情况下,这可能是可以接受的,但如果您想更改图标,请再次打开快捷方式的属性并单击更改图标...(再次,在Windows 7的快捷方式选项卡下)。此时,您可以浏览...查找图标或通过输入调出默认系统图标列表

    %SystemRoot%\system32\SHELL32.dll
    

    Browse...按钮的左侧并点击Enter。这适用于 Windows 7 和 Windows XP,但图标因样式更新而不同(但可以识别为相似)。根据Windows快捷所在的版本,该图标有时会发生相应的变化。

More Info:

更多信息:

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

请参阅使用带有传递给启动程序的参数的“start”命令,以更好地理解第一个Target命令开头的空双引号。

回答by Bob Pollack

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.

根据微软的说法,如果你将该'Start In'框留空,脚本将在当前工作目录中运行。我已经在 Windows 7 中尝试过这个,它似乎工作得很好。

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

来源:http: //support.microsoft.com/kb/283065

回答by TimDC

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

尝试使用相对(Windows 命令行应用程序)。

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

基本上,快捷方式可以有一个相对链接,但 Windows 无法实际制作一个。

回答by SqlRyan

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

如果您可以设置系统变量(例如 %MyGameFolder%),那么您可以在路径和快捷方式中使用它,Windows 将为您填充路径的其余部分(即 %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.

这是一个小入门。您可以通过批处理文件设置此值,或者如果您分享您计划如何创建快捷方式,也可以通过编程方式设置它。

回答by darda

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.

我喜欢leoj3n 的解决方案。它还可以用于设置相对的“开始于”目录,这是我使用start的 /D 参数所需要的。如果没有 /c 或 /k in 作为cmd的参数,则后续的启动命令不会运行。/c 将在运行命令后立即关闭 shell,而 /k 将使其保持打开状态(即使在命令完成后)。因此,如果您正在运行的任何内容都吐出标准并且您需要查看它,请使用 /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.

不幸的是,根据 lnk 文件规范,图标没有保存在快捷方式中,而是“使用环境变量编码,这使得可以在位置不同但使用环境变量表示的机器之间找到图标”。因此,如果路径发生变化并且您试图从指向的可执行文件中获取图标,则它可能无法正确传输。

回答by Charles P.

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:

您可以通过更改文件路径手动创建相对快捷方式。首先在通常的上下文菜单中,为文件和属性 -> 文件位置创建一个新的 Windows 快捷方式:

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

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

回答by Alexis B.

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.

在阅读了几个答案后,我决定用一个简单的解决方案来做到这一点:我没有使用快捷方式,而是创建了一个 .bat 文件,其中只有一行来调用主 .bat 文件,它可以像我想要的那样工作。

回答by Mehrdad Mirreza

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.

我不确定我是否正确,或者我遗漏了什么,但就目前而言(2016 年 7 月 11 日,运行 Win7 Enterprise SP1)LNK 文件在移动或什至更改驱动器号后自适应跑到一个新的地方!我在我的 USB 驱动器上创建了一个新的快捷方式,并尝试以相对位置保持不变的方式移动快捷方式及其目标,然后我更改了驱动器号。快捷方式在这两种情况下都有效,并且在我双击它后调整了目标字段。

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

看起来微软在过去的更新之一中解决了这个问题。

Please somebody confirm this.

请有人确认这一点。

回答by John Thow

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

按照您的方式制作快捷方式后,在属性中设置以下内容:

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

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

Start In: %cd%\data

目标: %comspec% /k "data\run.bat"

  • /k如果您不希望提示在运行后保持打开状态,请删除它。

开始于: %cd%\data

回答by zar

The link with a relative pathcan be created using the mklinkcommand on windows command line.

可以使用Windows 命令行上的mklink命令创建具有相对路径的链接。

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.

这可能是创建链接的最佳方式,因为显然,快捷方式的行为可能会根据它们的创建方式(UI 与 mklink 命令)而有所不同。当我更改根文件夹时,我观察到快捷方式的一些奇怪行为。

  • 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.
  • 我测试过的 Windows 7 上有一个奇怪的行为。有时,当目标的根文件夹更改时,链接仍然有效(快捷方式属性会自动更新以反映更改的路径!)。如果“开始于”字段在那里,它也会自动更新。
  • 我还注意到一个链接在我第一次更改根路径时不起作用(属性显示为旧),但它在第二次之后和之后的每次都有效。作为第一次运行的结果,链接属性得到更新!
  • 我还注意到至少有两个链接,它不会更新路径并且不再起作用。
  • 从链接属性来看,任何字段的格式没有区别,但行为不同。