使 Windows 快捷方式相对于文件夹所在位置启动?

发布于 2024-07-29 10:57:47 字数 197 浏览 12 评论 0 原文

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

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

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(17

一城柳絮吹成雪 2024-08-05 10:57:47
  1. 右键单击 /bat/ 文件夹,然后单击创建快捷方式

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

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

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

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

    调出默认系统图标列表

    <前><代码>%SystemRoot%\system32\SHELL32.dll

    浏览...按钮左侧,然后按Enter。 这适用于 Windows 7 和 Windows XP,但由于样式更新,图标有所不同(但可以看出是相似的)。 根据快捷方式所在的 Windows 版本,图标有时会相应更改。< /p>

更多信息:

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

  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.

晌融 2024-08-05 10:57:47

根据微软的说法,如果将“开始于”框留空,脚本将在当前工作目录中运行。 我已经在 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

§普罗旺斯的薰衣草 2024-08-05 10:57:47

如果您可以设置系统变量(例如 %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.

傻比既视感 2024-08-05 10:57:47

尝试使用 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.

歌入人心 2024-08-05 10:57:47

您可以通过更改文件路径手动创建相对快捷方式。
首先,在通常的上下文菜单中为文件创建一个新的 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"

如梦 2024-08-05 10:57:47

我不确定我是否正确,或者我遗漏了一些东西,但就目前而言(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.

旧情勿念 2024-08-05 10:57:47

创建快捷方式后,在属性中设置以下内容:

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

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

开始于:%cd%\data

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

紅太極 2024-08-05 10:57:47

阅读了几个答案后,我决定用一个简单的解决方案来做到这一点:
我没有使用快捷方式,而是制作了一个只有一行的 .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.

谎言 2024-08-05 10:57:47

我喜欢 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.

寒江雪… 2024-08-05 10:57:47

我在“开始于”字段中尝试了 %~dp0,它工作正常
在 Windows 10 x64 中

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

慕巷 2024-08-05 10:57:47

您可以让批处理文件更改当前工作目录 (CD)。

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

清风挽心 2024-08-05 10:57:47

可以使用相对路径的链接Windows 命令行上的 ="nofollow noreferrer">mklink 命令。

mklink /d \MyDocs \Users\User1\Documents

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

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

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.
亚希 2024-08-05 10:57:47

我已经尝试过你描述的许多方法,但不知何故在我的 W11 机器上不起作用。 所以我使用了 python 库来自动创建快捷方式。 如果您想使用 python 创建快捷方式,请执行以下操作:

如果未安装 pywin32,您可以使用 pip 安装它:

pip install pywin32

然后,请打开一个 python 文件并写入以下内容:

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

通过这种方法,我创建了快捷方式自动地..

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

我不吻晚风 2024-08-05 10:57:47

最简单的解决方案:>
环境变量
方便的小动物。

如果其他人要将任何内容安装/解压缩到其各自系统驱动器上的任何位置(通常是 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.

仙气飘飘 2024-08-05 10:57:47

只是对 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 adding start in front of your commands in run.bat. That will make the console window disappear, but everything else remains the same.

娇纵 2024-08-05 10:57:47

'leoj' 提出的方法不允许传递带空格的参数。
使用它:

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

这将是与路径中类似的双引号

    C:"\Program Files\anyProgram.exe" "Par1-1 Par1-2" Par2

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
我的黑色迷你裙 2024-08-05 10:57:47

建立相对路径的符号链接。 在命令提示符 cmd 上(具有管理员权限):

mklink /D SYMLINK_NAME RELATIVE_TARGET_PATH

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

mklink /D SYMLINK_NAME RELATIVE_TARGET_PATH
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文