首页 > 解决方案 > 删除快捷方式的脚本不适用于版本号

问题描述

我正在运行一个 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)

我感觉这与版本号和中间的小数点有关?谁能证实我的怀疑,因为我完全没有线索。

谢谢。

标签: vbscript

解决方案


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

您在桌面上看到的快捷方式不必位于您Desktop使用WshShell.SpecialFolders("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

推荐阅读