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

发布于 2024-11-08 21:00:26 字数 1846 浏览 0 评论 0原文

它在 wshShell.Run 上崩溃。

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

我尝试了 objFile.delete 但它说权限被拒绝。如果我在命令提示符中执行“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

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.CreateObject ("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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

泪冰清 2024-11-15 21:00:26

这应该可以做到:

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

This should do it:

wshShell.Run "del """ & objFile.Path & """", 1, True  
廻憶裏菂餘溫 2024-11-15 21:00:26

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文