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

标签 windows scripting vbscript windows-scripting

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

最佳答案

应该这样做:

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

关于windows - 我无法删除桌面上的文件(所有用户)但我可以在没有脚本的情况下删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6074177/

相关文章:

c++ - CreateThread 后跟 TerminateThread 留下大量内存

子进程中的 C++ windows LocalSystem 模拟失败

c# - 为什么 DirectoryInfo.GetFiles() 匹配与掩码不匹配的文件?

python - 通过 Python 脚本在 Maya 中将一个对象捕捉到另一个对象顶点

windows - 用于多服务器管理员的可移植脚本语言?

java - 如何编写命令行脚本(不是 ant 任务)来生成 checkstyle 报告?

ms-access - 以独占模式打开Access数据库

batch-file - 如何在输出文件名中包含主机名和日期?

vbscript - 崩溃时重新启动vbs脚本

c# - 在 Windows 资源管理器窗口中突出显示文件