【问题标题】:I am unable to delete a file on the Desktop (All Users) but I could delete without the script我无法删除桌面(所有用户)上的文件,但我可以在没有脚本的情况下删除
【发布时间】:2011-08-29 18:31:51
【问题描述】:

它在 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)

------------------ 更新 ------------------ 如果它在用户桌面(而不是 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

【问题讨论】:

  • 我在 Windows 7 上进行测试,但将部署在 Windows XP 上

标签: windows scripting vbscript windows-scripting


【解决方案1】:

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

【讨论】:

【解决方案2】:

应该这样做:

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

【讨论】:

  • 谢谢我复制并粘贴了你的行。这是输出。 (请参阅原始问题中的更新)
猜你喜欢
  • 2018-06-03
  • 2022-10-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-07
相关资源
最近更新 更多