我无法删除桌面上的文件(所有用户),但我可以删除没有脚本

时间:2011-05-20 15:29:55

标签: windows scripting vbscript windows-scripting

它在wshShell.Run上崩溃。

您可以看到我运行WScript.Echo并且它确实打印了文件名的位置。当我运行它时,它说“系统找不到指定的文件”

我试过objFile.delete,但它说Permission denied。如果我在命令提示符下执行“del”,它就可以工作。

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

输出

Lotus Notes 8.5.lnk
C:\Users\Public\Desktop\Lotus Notes 8.5.lnk
(null) (79, 3) : (null)

------------------ UPDATE ------------------ 如果它位于用户桌面(而不是AllUsersDesktop)上,则以下内容非常有效。我正试图从AllUsersDesktop中删除它

For Each objFile In colFiles
   bMatch = objRE.Test(objFile.Name)
   If bMatch Then
     objFile.Delete

   End If
Next

应用以下代码后,我收到此错误

Lotus Notes 8.5.lnk
C:\Users\Public\Desktop\Lotus Notes 8.5.lnk
(null) (81, 3) : (null)

代码:(自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

2 个答案:

答案 0 :(得分:1)

路径中有一个空格,所以它应该用双引号括起来,比如"del \"" & objFile.Path & "\"",或者用于转义的VB语法。

答案 1 :(得分:1)

这应该这样做:

wshShell.Run "del """ & objFile.Path & """", 1, True