VBScriptでゴミ箱のファイルを削除してみる

| 2009年6月20日土曜日
PowerShellではできたので、VBScriptで指定日以前にごみ箱に捨てたファイルを削除してみる。
PowerShellがもっと流行れば、VBScriptから逃げられるんだけど…その日はまだ遠そうだ。

※ごみ箱の中のフォルダを消そうとすると「実行時エラー:・・・」となるので一度Tempフォルダに移動してからcmdのrmdirで消している。

3日前に削除したファイルをけすなら、cmdなどから

> CScript /nologo DeleteGarbabeFiles.vbs /Date:3

とする。
/Date:3 をつけなければ、一週間前までのを削除する。

--- DeleteGarbabeFiles.vbs ---
  1. If WScript.Arguments.Named("Date") = "" Then  
  2.   nDays = 6  
  3. Else  
  4.   nDays = Int(WScript.Arguments.Named("Date"))  
  5. End If  
  6.   
  7. Set objFSO = CreateObject("Scripting.FileSystemObject")  
  8. Set objShell = CreateObject("Shell.Application")  
  9. Set WshShell = CreateObject("WScript.Shell")  
  10. Set trash = objShell.NameSpace(10)  
  11.   
  12. For Each item in trash.Items()  
  13.   If Int(Now() - CDate(trash.GetDetailsOf(item, 2))) >= nDays Then  
  14.     If objFSO.FileExists(item.Path) Then  
  15.       objFSO.DeleteFile item.Path  
  16.     ElseIf objFSO.FolderExists(item.Path) Then  
  17.       dustFolderPath = objFSO.GetSpecialFolder(2).Path & "_TempDeleteFolder"  
  18.       objFSO.MoveFolder item.Path, dustFolderPath  
  19.       delCommand = "cmd.exe /C rmdir /S /Q " & """" & dustFolderPath & """"   
  20.       WshShell.Run delCommand, 0, true  
  21.     End If  
  22.   End If  
  23. Next  
  24.   
  25. Set objFSO = Nothing  
  26. Set objShell = Nothing  
  27. Set WshShell = Nothing  
  28. Set trash = Nothing  

0 コメント: