如何为可执行文件创建link并在批处理或VBS中指定工作文件夹?

How to create link for executable file and indicate work folder in batch or VBS?

OS: Windows 7

我正在尝试像这样通过批处理创建一个 link 到桌面文件夹:

mklink "%userprofile%\Desktop\MyExe" "%~dp0\MyExe.exe" 

该命令有效,但如何指示 MyExe.exe"%~dp0" 执行? MyExe.exe 在当前文件夹中看起来像 运行,因此无法加载我的配置文件。

更新:

使用 VBS 遇到了不同的问题,运行 下面的代码将创建一个快捷方式 对于 C:\Users\jiu\Desktop\MyExe.exe,但我想要 MyExe.exe

Set oWS = WScript.CreateObject("WScript.Shell")
userProfilePath = oWS.ExpandEnvironmentStrings("%UserProfile%")
currParentFolder = CreateObject("Scripting.FileSystemObject").GetParentFolderName(WScript.ScriptFullName)
linkPath = userProfilePath + "\Desktop\MyExe.LNK"
Set oLink = oWS.CreateShortcut(linkPath)
    oLink.TargetPath = "MyExe.exe" 
    oLink.WorkingDirectory = currParentFolder
oLink.Save

mklink 不会创建 *.lnk shortcut but a symbolic/hard link 这只是物理文件的新名称。由于 link 在桌面上,显然如果您双击 link

当前文件夹将是桌面

您必须创建一个 shortcut。一种方法是使用下面的 vbs 脚本

Set oWS = WScript.CreateObject("WScript.Shell")
strDesktop = WshShell.SpecialFolders("Desktop")
sLinkFile = strDesktop & "\MyExe.lnk"
Set oLink = oWS.CreateShortcut(sLinkFile)

oLink.TargetPath = WScript.Arguments(0) & "\" & WScript.Arguments(1)
'  oLink.Arguments = ""
'  oLink.Description = "MyExe"
'  oLink.HotKey = "ALT+CTRL+F"
'  oLink.IconLocation = WScript.Arguments(0) & "\" & WScript.Arguments(1) & ", 2"
'  oLink.WindowStyle = "1"
   oLink.WorkingDirectory = WScript.Arguments(0)
oLink.Save

另存为然后调用

cscript "%~dp0" "MyExe.exe"

您也可以从 powershell or various other tools

创建它
$objShell = New-Object -ComObject WScript.Shell
$lnk = $objShell.CreateShortcut("$home\Desktop\MyExe.lnk")
$lnk.TargetPath = ".\MyExe.exe"
$lnk.Save()

这是我找到的:

Set oWS = WScript.CreateObject("WScript.Shell")
userProfilePath = oWS.ExpandEnvironmentStrings("%UserProfile%")
currParentFolder = CreateObject("Scripting.FileSystemObject").GetParentFolderName(WScript.ScriptFullName)
linkPath = userProfilePath + "\Desktop\MyExe.LNK"
targetPath = currParentFolder + "\MyExe.EXE"
Set oLink = oWS.CreateShortcut(linkPath)
    oLink.TargetPath = targetPath
    oLink.WorkingDirectory = currParentFolder
oLink.Save

根据您提供的信息,这里有一个看起来很奇怪的批处理文件,应该可以为您创建桌面快捷方式:

;@Rundll32 AdvPack.dll,LaunchINFSection "%~0",,1
;@GoTo :EOF
[Version]
Signature="$Windows NT$"
[DefaultInstall]
ProfileItems=AddLnk
[AddLnk]
Name="MyExe",8,16
CmdLine=1,,"MyExe.exe"
InfoTip="Execute MyExe.exe"
WorkingDir=1

您可以选择修改:

  • 快捷方式显示名称
    通过替换行 8.
  • 中双引号内的字符串
  • 目标可执行文件的名称
    通过替换第 9.
  • 行双引号内的字符串
  • 快捷键描述注释
    通过替换10.
  • 行双引号内的字符串