I am unable to delete a file on the Desktop (All Users) but I could delete without the script
It crashes on wshShell.Run.
You can see that I run a WScript.Echo and it does print the location of the file name. When I run it, it says the "The system could not find the file specified"
I tried objFile.delete but it says Permission denied. If i perform "del " in the command prompt, it works.
For Each objFile In colFiles
bMatch = objRE.Test(objFile.Name)
If bMatch Then
WScript.Echo objFile.Name
WScript.Echo objFile.Path
Set wshShell = WScript.CreateO开发者_开发问答bject ("WSCript.shell")
wshShell.Run "del " & objFile.Path, 1, True
Set wshShell = Nothing
End If
Next
Output
Lotus Notes 8.5.lnk
C:\Users\Public\Desktop\Lotus Notes 8.5.lnk
(null) (79, 3) : (null)
------------------ UPDATE ------------------ The following works perfectly if it's on the Users Desktop (not the AllUsersDesktop). I'm trying to delete it from the AllUsersDesktop
For Each objFile In colFiles
bMatch = objRE.Test(objFile.Name)
If bMatch Then
objFile.Delete
End If
Next
After applying the following code, I get this error
Lotus Notes 8.5.lnk
C:\Users\Public\Desktop\Lotus Notes 8.5.lnk
(null) (81, 3) : (null)
Code: (updated as of 5/23)
Set objShell = CreateObject("WScript.Shell")
strCurrentDirectory = objShell.SpecialFolders("AllUsersDesktop")
Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.Namespace(strCurrentDirectory)
Set objFolderItem = objFolder.Self
Set objFolder = objFS.GetFolder(strCurrentDirectory)
Set colFiles = objFolder.Files
Set objRE = New RegExp
objRE.Global = True
objRE.IgnoreCase = True
objRE.Pattern = "notes"
For Each objFile In colFiles
bMatch = objRE.Test(objFile.Name)
If bMatch Then
WScript.Echo objFile.Name
WScript.Echo objFile.Path
Set wshShell = WScript.CreateObject ("WSCript.shell")
wshShell.Run "del """ & objFile.Path & """", 1, True
Set wshShell = Nothing
End If
Next
This should do it:
wshShell.Run "del """ & objFile.Path & """", 1, True
The path has a space in it, so it should be enclosed in double quotes, something like "del \"" & objFile.Path & "\""
, or whatever VB syntax for escaping is.
精彩评论