背景
我有一个使用相对路径执行文件的 Windows 快捷方式。现在,我希望这个快捷方式有一个漂亮的图标。我已经有了图标图像,并将其放在bat
快捷方式指向的文件附近。
问题
这里的问题是我无法像设置那样设置快捷方式图标的路径target
。Windows 似乎有意让我为图标定义一个绝对路径,但我做不到,因为每台机器的路径都不一样。
所以我确实希望图标的路径也是相对的。
我尝试过使用%CD%
,但无法识别,尽管它在快捷方式中运行良好target
。
问题
如何在 Windows 中使用相对路径(快捷方式所在的位置)设置快捷方式图标?
答案1
正如评论中提到的,这是不是支持.lnk
快捷方式文件中的图标文件路径。Windows 不希望定期重新计算图标的路径。
正确的做法是创建自己的.exe
启动器文件并嵌入所需的图标。甚至还有文件.bat
到.exe
文件转换器工具,允许自定义图标。你可能想在软件推荐
答案2
对于完全动态的环境,我已经准备好了我认为可以使用的设置。一旦你在你的。蝙蝠文件,您需要运行一次,因此它会在您的桌面上创建一个快捷方式,之后您可以从快捷方式运行它。实际上,第一次必须“初始化”它。
我有四个图标可以动态更改快捷方式上的图标,虽然我可以在此示例中使用默认的 Windows 图标,但我将扩展根据路径更改图标的能力;
1. windows.ico -> https://icon-icons.com/downloadimage.php?id=27087&root=251/ICO/256/&file=Windows-01_27087.ico
2. progra_1.ico -> https://icon-icons.com/downloadimage.php?id=250639&root=3934/ICO/512/&file=default_program_folder_icon_250639.ico
3. users.ico -> https://icon-icons.com/downloadimage.php?id=14636&root=80/ICO/256/&file=user_accounts_15362.ico
4. default.ico -> https://icon-icons.com/downloadimage.php?id=21215&root=161/ICO/256/&file=default_22735.ico
此设置的目录位置无关紧要,它是你的文件位于。
我会假设你的。蝙蝠将始终位于安装 Windows 的同一磁盘上,如果不是,应该有很多“绕过”的示例。在大多数默认情况下,Windows 安装在 C: 驱动器上。Windows 将在命令 shell 中将该分区识别为 %systemdrive%。
对于初始设置,我将放置主要。蝙蝠文件到;
%systemdrive%\path\to\mybat
我将给它命名文件管理程序.此文件代表您的。蝙蝠文件,您只需在.bat 中添加一行;
cscript "%~dp0MyIconFix.vbs"
我将把其余代码放在 VBS 文件中并将其命名为我的图标修复工具
Set objShell = CreateObject("WScript.Shell")
scriptDir = Left(WScript.ScriptFullName, InStrRev(WScript.ScriptFullName, "\"))
Set objFSO = CreateObject("Scripting.FileSystemObject")
lnkName = "notetest.lnk"
batName = "MyFile.bat"
locDesktop = objShell.SpecialFolders("Desktop")
lnkPath = objFSO.BuildPath(locDesktop, lnkName)
UserOneD = objShell.ExpandEnvironmentStrings("%systemdrive%\Users\%username%\OneDrive")
Progra_1 = "C:\Program "
Progra_2 = "C:\Program"
Sys32Folder = objShell.ExpandEnvironmentStrings("%systemdrive%\Windows\System32")
WinFolder = objShell.ExpandEnvironmentStrings("%windir%")
UserDocs = objShell.ExpandEnvironmentStrings("%systemdrive%\Users\%username%\Documents")
If InStr(1, scriptDir, Progra_1) = 1 Then
iconPath = scriptDir & "icofolder\progra_1.ico"
ElseIf InStr(1, scriptDir, Progra_2) = 1 Then
iconPath = "%windir%\system32\imageres.dll,70"
ElseIf InStr(1, scriptDir, Sys32Folder) = 1 Then
iconPath = "%windir%\system32\imageres.dll, 9"
ElseIf InStr(1, scriptDir, WinFolder) = 1 Then
iconPath = scriptDir & "icofolder\windows.ico"
ElseIf InStr(1, scriptDir, UserDocs) = 1 Then
iconPath = scriptDir & "icofolder\users.ico"
ElseIf InStr(1, scriptDir, locDesktop) = 1 Then
iconPath = "%systemroot%\system32\imageres.dll,186"
ElseIf InStr(1, scriptDir, UserOneD) = 1 Then
iconPath = "%systemroot%\system32\imageres.dll,221"
Else
iconPath = "icofolder\default.ico"
End If
If (objFSO.FileExists(lnkPath)) Then
Set objShortcut1 = objShell.CreateShortcut(locDesktop & "\" & lnkName)
objShortcut1.TargetPath = scriptDir & batName
objShortcut1.WorkingDirectory = scriptDir
objShortcut1.IconLocation = iconPath
objShortcut1.Save
Else
Set objShortcut1 = objShell.CreateShortcut(locDesktop & "\" & lnkName)
objShortcut1.TargetPath = scriptDir & batName
objShortcut1.WindowStyle = 1
objShortcut1.WorkingDirectory = scriptDir
objShortcut1.Hotkey = "CTRL+SHIFT+Y"
objShortcut1.Description = "Shortcut to MyBat."
objShortcut1.IconLocation = iconPath
objShortcut1.Save
End If
您还可以添加一些日志,以防您想要跟踪您的路径,或者如果您将一些代码移动到脚本中,日志将在您的蝙蝠所在的同一位置生成;
debugOn = False
Set objShell = CreateObject("WScript.Shell")
scriptDir = Left(WScript.ScriptFullName, InStrRev(WScript.ScriptFullName, "\"))
Set objFSO = CreateObject("Scripting.FileSystemObject")
If debugOn Then
Set objLogFile = objFSO.CreateTextFile(scriptDir & "MyBat.Log", True)
objLogFile.WriteLine "Script Directory: " & scriptDir
End If
lnkName = "notetest.lnk"
batName = "MyFile.bat"
locDesktop = objShell.SpecialFolders("Desktop")
lnkPath = objFSO.BuildPath(locDesktop, lnkName)
UserOneD = objShell.ExpandEnvironmentStrings("%systemdrive%\Users\%username%\OneDrive")
Progra_1 = "C:\Program "
Progra_2 = "C:\Program"
Sys32Folder = objShell.ExpandEnvironmentStrings("%systemdrive%\Windows\System32")
WinFolder = objShell.ExpandEnvironmentStrings("%windir%")
UserDocs = objShell.ExpandEnvironmentStrings("%systemdrive%\Users\%username%\Documents")
If InStr(1, scriptDir, Progra_1) = 1 Then
iconPath = scriptDir & "icofolder\progra_1.ico"
If debugOn Then
objLogFile.WriteLine "FOUND:" & scriptDir & " matches the pattern C:\Program Files\"
End If
ElseIf InStr(1, scriptDir, Progra_2) = 1 Then
iconPath = "%windir%\system32\imageres.dll,70"
If debugOn Then
objLogFile.WriteLine scriptDir & " matches the pattern C:\ProgramData\"
End If
ElseIf InStr(1, scriptDir, Sys32Folder) = 1 Then
iconPath = "%windir%\system32\imageres.dll, 9"
If debugOn Then
objLogFile.WriteLine scriptDir & " matches the pattern C:\Windows\System32\"
End If
ElseIf InStr(1, scriptDir, WinFolder) = 1 Then
iconPath = scriptDir & "icofolder\windows.ico"
If debugOn Then
objLogFile.WriteLine scriptDir & " matches the pattern C:\Windows\"
End If
ElseIf InStr(1, scriptDir, UserDocs) = 1 Then
iconPath = scriptDir & "icofolder\users.ico"
If debugOn Then
objLogFile.WriteLine scriptDir & " matches the pattern C:\Users\*\Documents"
End If
ElseIf InStr(1, scriptDir, locDesktop) = 1 Then
iconPath = "%systemroot%\system32\imageres.dll,186"
If debugOn Then
expandUser = objShell.ExpandEnvironmentStrings(" matches the pattern C:\Users\%username%\Desktop")
objLogFile.WriteLine scriptDir & expandUser
End If
ElseIf InStr(1, scriptDir, UserOneD) = 1 Then
iconPath = "%systemroot%\system32\imageres.dll,221"
If debugOn Then
expandUser = objShell.ExpandEnvironmentStrings(" matches the pattern %UserOneD%")
objLogFile.WriteLine scriptDir & expandUser
End If
Else
iconPath = "icofolder\default.ico"
If debugOn Then
objLogFile.WriteLine scriptDir & " doesnt match any known folder, default ico applied!"
End If
End If
If debugOn Then
objLogFile.WriteLine "Icon Path: " & iconPath
objLogFile.WriteLine "Desk Path: " & locDesktop
objLogFile.WriteLine "lnk Name: " & lnkName
objLogFile.WriteLine "lnk Path: " & lnkPath
objLogFile.WriteLine "lnkScriptDir: " & scriptDir & lnkName
End If
If (objFSO.FileExists(lnkPath)) Then
Set objShortcut1 = objShell.CreateShortcut(locDesktop & "\" & lnkName)
objShortcut1.TargetPath = scriptDir & batName
objShortcut1.WorkingDirectory = scriptDir
objShortcut1.IconLocation = iconPath
objShortcut1.Save
If debugOn Then
objLogFile.WriteLine lnkPath & " exists on the desktop."
End If
Else
Set objShortcut1 = objShell.CreateShortcut(locDesktop & "\" & lnkName)
objShortcut1.TargetPath = scriptDir & batName
objShortcut1.WindowStyle = 1
objShortcut1.WorkingDirectory = scriptDir
objShortcut1.Hotkey = "CTRL+SHIFT+Y"
objShortcut1.Description = "Shortcut to MyBat."
objShortcut1.IconLocation = iconPath
objShortcut1.Save
If debugOn Then
objLogFile.WriteLine scriptDir & lnkName & " is created at:" & locDesktop
End If
End If
objLogFile.Close
现在您可以将整个文件夹移动到主驱动器上的任何位置。