批处理exe到快捷方式

Posted

技术标签:

【中文标题】批处理exe到快捷方式【英文标题】:Batch exe to shortcut 【发布时间】:2020-10-04 00:00:20 【问题描述】:

目前,我正在尝试为程序创建快捷方式,我能够做到这一点,如下面的代码所示。

echo Set oWS = WScript.CreateObject("WScript.Shell") > CreateShortcut.vbs
echo sLinkFile = "%HOMEDRIVE%%HOMEPATH%\Desktop\Unturned Dedicated Server\Unturned - Server.lnk" >> CreateShortcut.vbs
echo Set oLink = oWS.CreateShortcut(sLinkFile) >> CreateShortcut.vbs
echo oLink.TargetPath = "C:\Program Files (x86)\Steam\steamapps\common\Unturned\Unturned.exe" >> CreateShortcut.vbs
echo oLink.Save >> CreateShortcut.vbs
cscript CreateShortcut.vbs
del CreateShortcut.vbs

问题是我需要目标路径是"C:\Program Files (x86)\Steam\steamapps\common\Unturned\Unturned.exe" -batchmode -nographics +secureserver/ahhh

我该怎么做呢?

【问题讨论】:

您应该添加另一行来放置要传递给您的 .exe 目标的参数 试试echo oLink.Arguments = -batchmode -nographics +secureserver/ahhh >> CreateShortcut.vbs... 【参考方案1】:

您应该添加另一行来放置要传递给 .exe 目标的参数:

@echo off
Title Batch exe to shortcut
Set "VBS_Shortcut=%temp%\%~n0.vbs"
Set "ShortcutName=Unturned - Server"
Set "TargetPath=C:\Program Files (x86)\Steam\steamapps\common\Unturned\Unturned.exe"
Set "Arguments=-batchmode -nographics +secureserver/ahhh"
Call :Create_Shortcut "%ShortcutName%" "%TargetPath%" "%Arguments%"
Exit
REM ----------------------------------------------------------------------------------------------------
:Create_Shortcut
> "%VBS_Shortcut%" (
    echo    Call Create_Shortcut("%~1","%~2","%~3"^)
    echo    Sub Create_Shortcut(ShortcutName,TargetPath,Arguments^)
    echo        Dim objShell,DesktopPath,objShortCut
    echo        Set objShell = CreateObject("WScript.Shell"^)
    echo        DesktopPath = objShell.SpecialFolders("Desktop"^)
    echo        Set objShortCut = objShell.CreateShortcut(DesktopPath ^& "\" ^& ShortcutName ^& ".lnk"^)
    echo        objShortCut.TargetPath = chr(34^) ^& TargetPath ^& chr(34^)
    echo        objShortCut.Arguments = Arguments
    echo        objShortCut.Save
    echo    End Sub
)
cscript //nologo "%VBS_Shortcut%" "%~1" "%~2" "%~3"
If Exist "%VBS_Shortcut%" Del "%VBS_Shortcut%" 
Exit /B
REM ----------------------------------------------------------------------------------------------------

【讨论】:

【参考方案2】:

要以batch-file 的身份执行此操作,它会创建一个 vbscript,运行它,然后将其删除,我建议您这样做:

@(  Echo Set WshShell = WScript.CreateObject("WScript.Shell"^)
    Echo strDesktop = WshShell.SpecialFolders("Desktop"^)
    Echo str32bitPF = WshShell.ExpandEnvironmentStrings("%%ProgramFiles(x86)%%"^)
    Echo Set oFSO = CreateObject("Scripting.FileSystemObject"^)
    Echo If Not (oFSO.FolderExists(strDesktop + "\Unturned Dedicated Server"^)^) Then
    Echo   oFSO.CreateFolder(strDesktop + "\Unturned Dedicated Server"^)
    Echo End If
    Echo Set oShellLink = WshShell.CreateShortcut(strDesktop + "\Unturned Dedicated Server\Unturned - Server.lnk"^)
    Echo oShellLink.Arguments = "-batchmode -nographics +secureserver/ahhh"
    Echo oShellLink.TargetPath = str32bitPF + "\Steam\steamapps\common\Unturned\Unturned.exe"
    Echo oShellLink.WindowStyle = 1
    Echo oShellLink.Hotkey = "CTRL+SHIFT+U"
    Echo oShellLink.Description = "Launch Unturned"
    Echo oShellLink.WorkingDirectory = strDesktop + "\Unturned Dedicated Server"
    Echo oShellLink.Save) > "CreateShortcut.vbs"
@%__AppDir__%cscript.exe /NoLogo "CreateShortcut.vbs"
@Del "CreateShortcut.vbs"

但是,您也可以直接从您的 batch-file 执行此操作,而无需写入文件。

<!-- :
@%__AppDir__%cscript.exe /NoLogo "%~f0?.wsf"
@GoTo :EOF
-->
<Job><Script Language="VBScript">
Set WshShell = WScript.CreateObject("WScript.Shell")
strDesktop = WshShell.SpecialFolders("Desktop")
str32bitPF = WshShell.ExpandEnvironmentStrings("%ProgramFiles(x86)%")
Set oFSO = CreateObject("Scripting.FileSystemObject")
If Not (oFSO.FolderExists(strDesktop + "\Unturned Dedicated Server")) Then
oFSO.CreateFolder(strDesktop + "\Unturned Dedicated Server")
End If
Set oShellLink = WshShell.CreateShortcut(strDesktop + "\Unturned Dedicated Server\Unturned - Server.lnk")
oShellLink.Arguments = "-batchmode -nographics +secureserver/ahhh"
oShellLink.TargetPath = str32bitPF + "\Steam\steamapps\common\Unturned\Unturned.exe"
oShellLink.WindowStyle = 1
oShellLink.Hotkey = "CTRL+SHIFT+U"
oShellLink.Description = "Launch Unturned"
oShellLink.WorkingDirectory = strDesktop + "\Unturned Dedicated Server"
oShellLink.Save
</Script></Job>

如果您希望将自己的批处理文件代码添加到此版本,则必须将其插入到 @GoTo :EOF 行的正上方。

【讨论】:

以上是关于批处理exe到快捷方式的主要内容,如果未能解决你的问题,请参考以下文章

批处理创建快捷方式

帮忙制作一个bat:自动复制一个exe文件覆盖到指定文件夹,并把覆盖后的exe创建桌面快捷方式。

如何修改程序的图标(.exe后缀的)不是快捷方式的哦!

在 Windows 批处理文件中打开文本文件和程序快捷方式

如何自定义压缩文件中的快捷方式以从当前目录运行 exe?

win10下怎么在桌面创建IIS快捷方式