使 Windows 快捷方式相对于文件夹所在位置启动?
我有一个使用此文件结构的游戏:
GAME FOLDER
->data
->data->run.bat
我想在游戏文件夹中放置 run.bat
的快捷方式,但是如果我移动它,或者其他人安装它,它将无法工作,因为目标错误。 有没有办法使目标和“开始”相对于游戏文件夹?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(17)
右键单击 /bat/ 文件夹,然后单击创建快捷方式。
bat - Shortcut
。bat 快捷方式
。右键单击您刚刚创建的快捷方式,然后单击属性。
将目标(在 Windows 7 上的快捷方式选项卡下)更改为以下内容:
确保开始于 > 为空。 这会导致它在当前目录中启动。
在快捷方式为 .bat 的情况下这可能是可以接受的,但如果您想更改图标,请再次打开快捷方式的属性并单击更改图标...(再次在 Windows 7 上的 >快捷方式 选项卡)。 此时,您可以浏览...查找图标或通过输入
调出默认系统图标列表
<前><代码>%SystemRoot%\system32\SHELL32.dll
在
浏览...
按钮左侧,然后按Enter。 这适用于 Windows 7 和 Windows XP,但由于样式更新,图标有所不同(但可以看出是相似的)。 根据快捷方式所在的 Windows 版本,图标将有时会相应更改。< /p>更多信息:
请参阅使用“start”命令并将参数传递给启动的程序,以更好地理解第一个 Target 命令开头的空双引号。
Right click on your /bat/ folder and click Create Shortcut.
bat - Shortcut
in the current directory.Shortcut to bat
.Right click on the shortcut you just created and click Properties.
Change Target (under the Shortcut tab on Windows 7) to the following:
Make sure Start in is blank. That causes it to start in the current directory.
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
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 iconwillwill 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.
根据微软的说法,如果将“开始于”框留空,脚本将在当前工作目录中运行。 我已经在 Windows 7 中尝试过此操作,效果似乎很好。
来源:http://support.microsoft.com/kb/283065
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
如果您可以设置系统变量(例如 %MyGameFolder%),那么您可以在路径和快捷方式中使用它,Windows 将为您填写路径的其余部分(即
%MyGameFolder%\data\ MyGame.exe
)。这是一个小入门。 您可以通过批处理文件设置此值,或者如果您分享计划如何创建快捷方式,则可以通过编程方式设置它。
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.
尝试使用 Relative(Windows 命令行应用程序)。
基本上,快捷方式可以有一个相对链接,但 Windows 没有提供实际创建一个相对链接的方法。
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.
您可以通过更改文件路径手动创建相对快捷方式。
首先,在通常的上下文菜单中为文件创建一个新的 Windows 快捷方式,然后在属性 -> 中创建一个新的快捷方式。 文件位置:
%windir%\explorer.exe "..\data\run.bat"
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"
我不确定我是否正确,或者我遗漏了一些东西,但就目前而言(2016年7月11日,运行Win7 Enterprise SP1),LNK文件会在移动甚至更改驱动器盘符后进行自我调整。去新的地方奔跑吧!
我在 USB 驱动器上创建了一个新的快捷方式,并尝试以相对位置保持不变的方式移动快捷方式及其目标,然后更改了驱动器号。 该快捷方式在这两种情况下都有效,并且在我双击它后目标字段进行了调整。
看来微软已经在过去的一次更新中解决了这个问题。
请有人确认这一点。
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.
创建快捷方式后,在属性中设置以下内容:
After making the shortcut as you have, set the following in Properties:
阅读了几个答案后,我决定用一个简单的解决方案来做到这一点:
我没有使用快捷方式,而是制作了一个只有一行的 .bat 来调用主 .bat,它的工作方式就像我想要的那样。
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.
我喜欢 leoj3n 的解决方案。 它还可用于设置相对“开始于”目录,这是我使用 start< 所需要的/a> 的 /D 参数。 如果没有 /c 或 /k 作为 cmd 的参数,则后续启动命令不会跑步。 /c 将在运行命令后立即关闭 shell,而 /k 将使其保持打开状态(即使在命令完成后)。 因此,如果您正在运行的任何内容都符合标准输出并且您需要查看它,请使用/k。
不幸的是,根据lnk文件 规范,图标不保存在快捷方式中,而是“使用环境变量进行编码”,这样就可以跨位置不同但使用环境变量表示的机器找到图标。 ” 因此,如果路径发生变化并且您尝试从您指向的可执行文件中获取图标,它很可能无法正确传输。
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.
我在“开始于”字段中尝试了 %~dp0,它工作正常
在 Windows 10 x64 中
I tried %~dp0 in the Start in field and it is working fine
in Windows 10 x64
您可以让批处理文件更改当前工作目录 (CD)。
You could have the batch file change the current working directory (CD).
可以使用相对路径的链接Windows 命令行上的 ="nofollow noreferrer">mklink 命令。
这可能是创建链接的最佳方法,因为显然,快捷方式的行为可能会有所不同,这可能取决于它们的创建方式(UI 与 mklink 命令)。 当我更改根文件夹时,我观察到快捷方式的一些奇怪行为。
The link with a relative path can be created using the mklink command on windows command line.
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.
我已经尝试过你描述的许多方法,但不知何故在我的 W11 机器上不起作用。 所以我使用了 python 库来自动创建快捷方式。 如果您想使用 python 创建快捷方式,请执行以下操作:
如果未安装 pywin32,您可以使用 pip 安装它:
然后,请打开一个 python 文件并写入以下内容:
通过这种方法,我创建了快捷方式自动地..
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:Then, please open a python file and write following:
With this method, I created shortcuts automatically..
最简单的解决方案:>
环境变量
方便的小动物。
如果其他人要将任何内容安装/解压缩到其各自系统驱动器上的任何位置(通常是 c:)。
出于演示目的,请调用我们的应用程序“test.exe”(可以是任何可执行文件/文件,不一定是 exe),并将其安装/解压缩到文件夹 MYCOMPANY\MYAPP\
然后只需创建一个使用 %SystemDrive% 的快捷方式\MYCOMPANY\MYAPP\test.exe 作为目标并且
%SystemDrive%\MYCOMPANY\MYAPP\ 作为启动。
所以现在您想要部署它。
使用“WinRAR”等应用程序。
简单的方法是使用自解压 zip 文件,整齐地打包为“.exe”
我会使用一个作为我的快捷方式,另一个作为应用程序。 有多种方法可以制作一个自解压 zip 文件,将不同的文件解压到不同的目录,但我还没有使用它。
另一种方法是为快捷方式创建自解压,将其嵌入到应用程序的自解压中,然后应用运行一次脚本,这样您就知道文件将在哪里。
等等。
如果您想让安装程序使用自定义安装/解压缩目录,请查看 NSIS 一个可编写脚本的安装系统。
玩起来很有趣,希望我的信息有帮助。
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.
只是对 leoj3n 解决方案的一个小改进(使控制台窗口消失):而不是将
%windir%\system32\cmd.exe /c start "" "%CD%\bat\bat\run.bat"< /code> 到 Windows 快捷方式的 Target: 字段,您还可以尝试仅添加以下内容:
%windir%\system32\cmd.exe /c "%CD%\bat\ bat\run.bat"
并且然后在run.bat
中的命令前面添加start
。 这将使控制台窗口消失,但其他一切保持不变。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 addingstart
in front of your commands inrun.bat
. That will make the console window disappear, but everything else remains the same.'leoj' 提出的方法不允许传递带空格的参数。
使用它:
这将是与路径中类似的双引号
The method that proposed by 'leoj' does not allow passing parameters with spaces.
Us it:
Which will be similar double quote written as in path
建立相对路径的符号链接。 在命令提示符
cmd
上(具有管理员权限):Make a symbolic link of a relative path. On the command prompt
cmd
(with Administrator privileges):