Automatically cleaning up your hard disk

Windows applications usually creates backup or temporary files in different directories on your hard drive. Some of them are automatically erased by corresponding applications while others stay in your hard drive for a long time if not forever. These files are garbage in your hard drive. It is hard to clean them up at one time. You have to use Windows Explorer to find and delete them manually. Windows Script Host provides us a potential to write a short script to execute repetitive jobs.

After I read an article in PC Magazine, I wrote a Windows script file to clean my hard disk. The following VB script file can clean Windows temporary folder, recycle bin, and three specific temporary files with the extensions of .~, .BAK, and .$$$. It runs in the background. You can put it on your desktop. If you need to clean your hard drive, just double click it. The source code is listed below.

Option Explicit
Dim FSO, WshShell, WSHShellENV, TempFolder, Recycled
On Error Resume Next
Set FSO = CreateObject("Scripting.FileSystemObject")
set WshShell = WScript.CreateObject("WScript.Shell")
Set WSHShellENV = WshShell.Environment("PROCESS")
set TempFolder = FSO.GetFolder(WSHShellENV("TEMP"))
ClearFolder TempFolder
Set Recycled = FSO.GetFolder("C:\recycled")
ClearFolder Recycled
DoDir FSO.GetFolder("c:\")
WScript.Echo "Cleaning job finished"

Sub DoDir(Folder)
   Dim i, File, SubFolder, fstr, pos
   Dim Findstr(2)
   Findstr(0) = ".$$$"
   Findstr(1) = ".BAK"
   Findstr(2) = ".~??"
   For Each File In Folder.Files
      FStr = UCase(File.Path)
      Pos = 0
      for i = 0 to 2
      	if instr(FStr, FindStr(i)) > 0 then
      	   File.delete
      	   Exit For
      	End if
      Next
   Next
   For Each SubFolder in Folder.SubFolders
      DoDir SubFolder
   Next
End Sub

Sub ClearFolder(Folder)
   Dim File, SubFolder
   For each file in Folder.Files
   	File.delete(True)
   next
   For Each SubFolder in Folder.SubFolders
      SubFolder.Delete(True)
   Next
End Sub

Reference

  • Share/Bookmark

Leave a Response

You must be logged in to post a comment.