删除快捷方式的脚本不适用于版本号

Script to remove shortcuts won't work with version numbers

我是 运行 一个 vbscript,用于删除另一个程序安装的桌面快捷方式,但我遇到的问题是一个快捷方式很顽固

set WshShell = WScript.CreateObject("WScript.Shell" ) 
strDesktop = WshShell.SpecialFolders("Desktop" )
' delete this shortcut
strShortcut = strDesktop & "\Shortcut Name 2.0.lnk"

Set fso = CreateObject("Scripting.FileSystemObject")
If fso.FileExists(strShortcut) Then fso.DeleteFile(strShortcut)

我感觉跟版本号和中间的小数点有关?任何人都可以证实我的怀疑,因为我完全没有线索。

谢谢。

您要删除的文件可能不存在,If fso.FileExists(strShortcut) Then掩盖了这一事实。

您在桌面上看到的快捷方式不一定位于您使用 WshShell.SpecialFolders("Desktop") 获得的 Desktop 文件夹中。

还有一个位置,桌面项目作为所有用户桌面目录的扩展存储,但可能需要管理员权限才能修改,我不确定,你需要试试。

所以,除了SpecialFolders("Desktop"),你还应该考虑SpecialFolders("AllUsersDesktop")目录。

Set Fso = CreateObject("Scripting.FileSystemObject")
Set WshShell = WScript.CreateObject("WScript.Shell")

userDesktop = WshShell.SpecialFolders("Desktop")
publicDesktop = WshShell.SpecialFolders("AllUsersDesktop")

shortcutName = "Shortcut Name 2.0.lnk"

userShortcut = Fso.BuildPath(userDesktop, shortcutName)
publicShortcut = Fso.BuildPath(publicDesktop, shortcutName)

If Fso.FileExists(userShortcut) Then 
    Fso.DeleteFile userShortcut, True
    MsgBox "User shortcut deleted."
End If

If Fso.FileExists(publicShortcut) Then
    Fso.DeleteFile publicShortcut, True
    MsgBox "Public shortcut deleted."
End If