File Management via Windows Scripting
Figure 1: File deletion script.
Continued from A Few Details
This script lets you designate by extension a set of files to delete.
Download this script as part of this issue's archive.
Dim FSO, WSH, TheExtension, TheCount
Dim TheFolder, Message, YesNo
Set FSO = CreateObject("Scripting.FileSystemObject")
Set WSH = CreateObject("WScript.Shell")
Set TheFolder = FSO.GetFolder("C:\") 'start at the root of C
TheExtension = InputBox("What extension to delete-don't use a '.', example: tmp")
TheExtension = UCase(TheExtension)
Message = "Are you sure you want to delete ALL files in ALL sub-directories with "
Message = Message & "the extension " & vbCrLf
Message = Message & vbCrLf & vbTab & vbTab & vbTab & TheExtension
YesNo = MsgBox(Message, vbYesNo)
If YesNo = vbYes Then
Message = "Press Enter or click on OK to start the process. "
Message = Message & "Note, this can take some time. A message "
Message = Message & "box will appear for one second after each "
Message = Message & "100 sub-directories are processed. "
Message = Message & "It will vanish after 1 second automatically"
MsgBox Message
WorkWithSubFolders TheFolder, TheExtension
End If
MsgBox "Done! Worked on " & TheCount & " directories. Click on OK or press Enter to end."
Sub WorkWithSubFolders(ByVal AFolder, ByVal TheExtension)
Dim MoreFolders, TempFolder
TheCount = TheCount + 1 'VBScript has no static variables,
'so use a global variable
If TheCount Mod 100 = 0 Then
Message = "Have worked so far with " & TheCount & " folders."
WSH.Popup Message, 1,"File deletion at work" 'super message box 1 second delay
End If
KillFilesWithExtensionIn AFolder, TheExtension
Set MoreFolders = AFolder.SubFolders
For Each TempFolder In MoreFolders
WorkWithSubFolders TempFolder, TheExtension
Next
End Sub
Sub KillFilesWithExtensionIn(AFolder,TheExtension)
Dim AFile, TheFiles
On Error Resume Next
Set TheFiles = AFolder.Files
For Each AFile In TheFiles
If UCase(FSO.GetExtensionName(AFile.Path)) = TheExtension Then
AFile.Delete
End If
Next
End Sub
Next: Figure 2: VBScript's File-Handling Objects
Published as Operating Systems in the 6/30/98 issue of PC Magazine.