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