2011-05-20 28 views
1

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

我測試在Windows 7,但將被部署用於Windows XP – 2011-05-23 14:56:05

回答

1

這應做到:

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

謝謝我複製並粘貼你的行。這裏是輸出。(請參閱原始問題更新) – 2011-05-23 14:53:38

1

該路徑中有一個空格,所以它應該用雙引號引起來,比如"del \"" & objFile.Path & "\"",或者任何VB s yntax轉義是。

+0

我試過雙「上http://answers.yahoo.com/question/index提到? qid = 1006042418111,我已經嘗試過單個',如http://arstechnica.com/civis/viewtopic.php?f=20&t=662368所述。在這兩種情況下,我都得到了同樣的錯誤(「系統找不到文件指定「) – 2011-05-20 15:39:47

相關問題