你如何通过批处理文件设置快捷方式的图标?
how do you set the icon of a shortcut through a batch file?
我正在尝试创建一个脚本来创建目标批处理文件的快捷方式并将图标设置为 .ico 或 .dll 图标文件:
@echo off
@echo Writing CreateShortcut script
@echo Set oWS = WScript.CreateObject("WScript.Shell") >> CreateShortcut.vbs
@echo sLinkFile = "C:\rustserver\Batch Files\rustserver\launcher\Test1.lnk" >> CreateShortcut.vbs
@echo Set oLink = oWS.CreateShortcut(sLinkFile) >> CreateShortcut.vbs
@echo oLink.TargetPath = "C:\rustserver\Batch Files\Test1.bat" >> CreateShortcut.vbs
@echo oLink.Save >> CreateShortcut.vbs
pause
@echo Writing EditShortcut script
@echo Set iWS = WScript.CreateObject("Shell.Application") >> EditShortcut.vbs
@echo sIconFile.IconLocation "C:\rustserver\Batch Files\rustserver\launcher\Icons\CompilerIcon.ico",0 >> EditShortcut.vbs
@echo Set oIcon = iWS.EditShortcut(sIconFile) >> EditShortcut.vbs
@echo oIcon.TargetPath = "C:\rustserver\Batch Files\rustserver\launcher\Test1.lnk" >> EditShortcut.vbs
@echo oIcon.Save >> EditShortcut.vbs
pause
@echo Running .vbs scripts...
cscript CreateShortcut.vbs
cscript EditShortcut.vbs
pause
@echo Deleting .vbs scripts...
del CreateShortcut.vbs
del EditShortcut.vbs
pause
@echo Deleting test shortcuts...
del Test1.lnk
pause
不能将 .bat 文件转换为 .exe,因为每个批处理文件之间有一些值需要重新配置。
就像我可以获取脚本来创建批处理文件的快捷方式一样,但无论我尝试什么,我都无法获取脚本来设置快捷方式的图标...
“未被识别为内部或外部命令。”是由于示例中的斜引号。将斜引号更改为直引号,然后重试。像这样:
Const DESKTOP = &H10&
Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.NameSpace(DESKTOP)
Set objFolderItem = objFolder.ParseName("Test Shortcut.lnk")
Set objShortcut = objFolderItem.GetLink
objShortcut.SetIconLocation "C:\Windows\System32\SHELL32.dll", 13
objShortcut.Save
注意:OP 在我回答之后(或同时)编辑了问题。请查看原问题。
想通了。对于遇到同样问题的任何人,我会在下面留下脚本的工作版本,如果你做同样的事情,我应该能够理解并相应地修改它。
@echo off
@echo Set oWS = WScript.CreateObject("WScript.Shell") >> CreateShortcut.vbs
@echo Set oSA = CreateObject("Shell.Application") >> EditShortcut.vbs
@echo sLinkFile = "C:\rustserver\Batch Files\rustserver\launcher\Test1.lnk" >> CreateShortcut.vbs
@echo Set oLink = oWS.CreateShortcut(sLinkFile) >> CreateShortcut.vbs
@echo oLink.TargetPath = "C:\rustserver\Batch Files\Test1.bat" >> CreateShortcut.vbs
@echo oLink.Save >> CreateShortcut.vbs
@echo Set objShell = CreateObject("Shell.Application") >> EditShortcut.vbs
@echo Set objFolder = objShell.NameSpace("C:\rustserver\") >> EditShortcut.vbs
@echo Set objFolderItem = objFolder.ParseName("Batch Files\rustserver\launcher\Test1.lnk") >> EditShortcut.vbs
@echo Set objShortcut = objFolderItem.GetLink >> EditShortcut.vbs
@echo objShortcut.SetIconLocation "C:\rustserver\Batch Files\rustserver\launcher\Icons\SMIcons.dll", 0 >> EditShortcut.vbs
@echo objShortcut.Save >> EditShortcut.vbs
cscript CreateShortcut.vbs
cscript EditShortcut.vbs
del CreateShortcut.vbs
del EditShortcut.vbs
鉴于您自己发布的解决方案,您可以通过批处理文件以更简单的方式完成。
下面的代码打开你的文件只写一次,然后关闭它,而你的文件是对每一行都这样做的。你只需要一个vbscript,创建快捷方式,你不需要创建一个,稍后再编辑。
::
@( Echo Set WshShell = WScript.CreateObject("WScript.Shell"^)
Echo Set oShellLink = WshShell.CreateShortcut("C:\rustserver\Batch Files\rustserver\launcher\Test1.lnk"^)
Echo oShellLink.TargetPath = """C:\rustserver\Batch Files\Test1.bat"""
Echo oShellLink.WindowStyle = 1
Echo oShellLink.Hotkey = "CTRL+SHIFT+B"
Echo oShellLink.IconLocation = "C:\rustserver\Batch Files\rustserver\launcher\Icons\SMIcons.dll, 0"
Echo oShellLink.Description = "Run Windows Command Script"
Echo oShellLink.WorkingDirectory = "C:\rustserver\Batch Files"
Echo oShellLink.Save) 1>"CreateShortcut.vbs"
@%SystemRoot%\System32\cscript.exe //NoLogo "CreateShortcut.vbs" && Del "CreateShortcut.vbs"
使用上述方法要记住的主要事情是,因为重定向的写入块是带括号的,所以您需要转义任何嵌套的右括号,(带插入符) , 以防止外部块被过早关闭。
然而,有一种更简单的方法可以做到这一点,它不需要您转义任何内容,也不需要动态写入,运行,然后删除另一个文件。您可以使用 batch/WSH 多语言:
<!-- ::
@%SystemRoot%\System32\cscript.exe //NoLogo "%~f0?.wsf"
@Exit /B
-->
<Job><Script Language="VBScript">
Set WshShell = WScript.CreateObject("WScript.Shell")
Set oShellLink = WshShell.CreateShortcut("C:\rustserver\Batch Files\rustserver\launcher\Test1.lnk")
oShellLink.TargetPath = """C:\rustserver\Batch Files\Test1.bat"""
oShellLink.WindowStyle = 1
oShellLink.Hotkey = "CTRL+SHIFT+B"
oShellLink.IconLocation = "C:\rustserver\Batch Files\rustserver\launcher\Icons\SMIcons.dll, 0"
oShellLink.Description = "Run Windows Command Script"
oShellLink.WorkingDirectory = "C:\rustserver\Batch Files"
oShellLink.Save
</Script></Job>
我正在尝试创建一个脚本来创建目标批处理文件的快捷方式并将图标设置为 .ico 或 .dll 图标文件:
@echo off
@echo Writing CreateShortcut script
@echo Set oWS = WScript.CreateObject("WScript.Shell") >> CreateShortcut.vbs
@echo sLinkFile = "C:\rustserver\Batch Files\rustserver\launcher\Test1.lnk" >> CreateShortcut.vbs
@echo Set oLink = oWS.CreateShortcut(sLinkFile) >> CreateShortcut.vbs
@echo oLink.TargetPath = "C:\rustserver\Batch Files\Test1.bat" >> CreateShortcut.vbs
@echo oLink.Save >> CreateShortcut.vbs
pause
@echo Writing EditShortcut script
@echo Set iWS = WScript.CreateObject("Shell.Application") >> EditShortcut.vbs
@echo sIconFile.IconLocation "C:\rustserver\Batch Files\rustserver\launcher\Icons\CompilerIcon.ico",0 >> EditShortcut.vbs
@echo Set oIcon = iWS.EditShortcut(sIconFile) >> EditShortcut.vbs
@echo oIcon.TargetPath = "C:\rustserver\Batch Files\rustserver\launcher\Test1.lnk" >> EditShortcut.vbs
@echo oIcon.Save >> EditShortcut.vbs
pause
@echo Running .vbs scripts...
cscript CreateShortcut.vbs
cscript EditShortcut.vbs
pause
@echo Deleting .vbs scripts...
del CreateShortcut.vbs
del EditShortcut.vbs
pause
@echo Deleting test shortcuts...
del Test1.lnk
pause
不能将 .bat 文件转换为 .exe,因为每个批处理文件之间有一些值需要重新配置。
就像我可以获取脚本来创建批处理文件的快捷方式一样,但无论我尝试什么,我都无法获取脚本来设置快捷方式的图标...
“未被识别为内部或外部命令。”是由于示例中的斜引号。将斜引号更改为直引号,然后重试。像这样:
Const DESKTOP = &H10&
Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.NameSpace(DESKTOP)
Set objFolderItem = objFolder.ParseName("Test Shortcut.lnk")
Set objShortcut = objFolderItem.GetLink
objShortcut.SetIconLocation "C:\Windows\System32\SHELL32.dll", 13
objShortcut.Save
注意:OP 在我回答之后(或同时)编辑了问题。请查看原问题。
想通了。对于遇到同样问题的任何人,我会在下面留下脚本的工作版本,如果你做同样的事情,我应该能够理解并相应地修改它。
@echo off
@echo Set oWS = WScript.CreateObject("WScript.Shell") >> CreateShortcut.vbs
@echo Set oSA = CreateObject("Shell.Application") >> EditShortcut.vbs
@echo sLinkFile = "C:\rustserver\Batch Files\rustserver\launcher\Test1.lnk" >> CreateShortcut.vbs
@echo Set oLink = oWS.CreateShortcut(sLinkFile) >> CreateShortcut.vbs
@echo oLink.TargetPath = "C:\rustserver\Batch Files\Test1.bat" >> CreateShortcut.vbs
@echo oLink.Save >> CreateShortcut.vbs
@echo Set objShell = CreateObject("Shell.Application") >> EditShortcut.vbs
@echo Set objFolder = objShell.NameSpace("C:\rustserver\") >> EditShortcut.vbs
@echo Set objFolderItem = objFolder.ParseName("Batch Files\rustserver\launcher\Test1.lnk") >> EditShortcut.vbs
@echo Set objShortcut = objFolderItem.GetLink >> EditShortcut.vbs
@echo objShortcut.SetIconLocation "C:\rustserver\Batch Files\rustserver\launcher\Icons\SMIcons.dll", 0 >> EditShortcut.vbs
@echo objShortcut.Save >> EditShortcut.vbs
cscript CreateShortcut.vbs
cscript EditShortcut.vbs
del CreateShortcut.vbs
del EditShortcut.vbs
鉴于您自己发布的解决方案,您可以通过批处理文件以更简单的方式完成。
下面的代码打开你的文件只写一次,然后关闭它,而你的文件是对每一行都这样做的。你只需要一个vbscript,创建快捷方式,你不需要创建一个,稍后再编辑。
::
@( Echo Set WshShell = WScript.CreateObject("WScript.Shell"^)
Echo Set oShellLink = WshShell.CreateShortcut("C:\rustserver\Batch Files\rustserver\launcher\Test1.lnk"^)
Echo oShellLink.TargetPath = """C:\rustserver\Batch Files\Test1.bat"""
Echo oShellLink.WindowStyle = 1
Echo oShellLink.Hotkey = "CTRL+SHIFT+B"
Echo oShellLink.IconLocation = "C:\rustserver\Batch Files\rustserver\launcher\Icons\SMIcons.dll, 0"
Echo oShellLink.Description = "Run Windows Command Script"
Echo oShellLink.WorkingDirectory = "C:\rustserver\Batch Files"
Echo oShellLink.Save) 1>"CreateShortcut.vbs"
@%SystemRoot%\System32\cscript.exe //NoLogo "CreateShortcut.vbs" && Del "CreateShortcut.vbs"
使用上述方法要记住的主要事情是,因为重定向的写入块是带括号的,所以您需要转义任何嵌套的右括号,(带插入符) , 以防止外部块被过早关闭。
然而,有一种更简单的方法可以做到这一点,它不需要您转义任何内容,也不需要动态写入,运行,然后删除另一个文件。您可以使用 batch/WSH 多语言:
<!-- ::
@%SystemRoot%\System32\cscript.exe //NoLogo "%~f0?.wsf"
@Exit /B
-->
<Job><Script Language="VBScript">
Set WshShell = WScript.CreateObject("WScript.Shell")
Set oShellLink = WshShell.CreateShortcut("C:\rustserver\Batch Files\rustserver\launcher\Test1.lnk")
oShellLink.TargetPath = """C:\rustserver\Batch Files\Test1.bat"""
oShellLink.WindowStyle = 1
oShellLink.Hotkey = "CTRL+SHIFT+B"
oShellLink.IconLocation = "C:\rustserver\Batch Files\rustserver\launcher\Icons\SMIcons.dll, 0"
oShellLink.Description = "Run Windows Command Script"
oShellLink.WorkingDirectory = "C:\rustserver\Batch Files"
oShellLink.Save
</Script></Job>