2014-01-08 52 views
0

我需要幫助這個腳本。此腳本創建一個新的桌面快捷方式,然後刪除另一個。不過,我希望它刪除一個桌面快捷方式(如果存在)或另一個(如果存在)。不知道如何做到這一點。我在「fso.deletefile」後面放了兩個不同的快捷方式,但我不確定要使用什麼語法(如何說出它)。我是vbs的新手。先謝謝您的幫助。VB腳本刪除桌面快捷方式

L_Welcome_MsgBox_Message_Text = "A shortcut to the PM Master" & vbcrlf & "will be created on your desktop." 
L_Welcome_MsgBox_Title_Text ="Windows Scripting Host Sample" 

Call Welcome() 

Dim WSHShell 
Set WSHShell =CreateObject("WScript.Shell") 

Dim MyShortcut, MyDesktop, DesktopPath 
' Read desktop path using WshSpecialFolders object 
DesktopPath =WSHShell.SpecialFolders("Desktop") 
' Create a shortcut object on the desktop 
Set MyShortcut =WSHShell.CreateShortcut(DesktopPath & "\PM-Master-ALL.lnk") 
' Set shortcut object properties and save it 
MyShortcut.TargetPath =WSHShell.ExpandEnvironmentStrings("c:\Local Cloud\Shared\Sites\Bailey Lane\PM-Master-ALL") 
MyShortcut.Save 
Set Shell = CreateObject("WScript.Shell") 
Set FSO = CreateObject("Scripting.FileSystemObject") 
DesktopPath = Shell.SpecialFolders("Desktop") 
FSO.DeleteFile DesktopPath & "\PM Master - ALL.lnk" 
FSO.DeleteFile DesktopPath & "\PM Master - ALL - Shortcut.lnk" 


WScript.Echo "A shortcut to the PM Master has been successfully created. The older PM Master shortcut has been deleted." 

Sub Welcome() 
    Dim intDoIt 
    intDoIt = MsgBox(L_Welcome_MsgBox_Message_Text, vbOKCancel + vbInformation, L_Welcome_MsgBox_Title_Text) 
    If intDoIt = vbCancel Then 
    WScript.Quit 

End If 
End Sub 

回答

2

如有疑問,請閱讀documentation。在嘗試刪除文件之前,您可以使用FileExists方法檢查文件是否存在:

shortcut = DesktopPath & "\PM Master - ALL.lnk" 
If FSO.FileExists(shortcut) Then FSO.DeleteFile shortcut 
相關問題