Introduction
This is a very simple code taken mostly from VS help pages. It's not fancy but does its job well.
Background
Some people seem to make a big deal out of recursive processing of directory trees. It really is rather simple. Using a combination
of DirectoryInfo and FileInfo objects, directory "walking" can be accomplished with a single module.
Using the Code
The DirectoryInfo class has very convenient methods for processing directories. It does have one problem, it can not read our minds and needs
an initial directory to start processing. If you wanted to process all directories on a machine, use the DriveInfo class to get the root directories
of all drives and pass that to DirectoryInfo.
Now for an example:
Public Sub DeleteFiles(ByVal targetDirectory As String)
Dim di As New DirectoryInfo(targetDirectory)
Dim fileEntries As FileInfo() = di.GetFiles()
Dim fileName As FileInfo
For Each fileName In fileEntries
If fileName.Length <= 1024 Then
Try
fileName.Delete()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End If
Next fileName
Dim subdirectoryEntries As String() = _
Directory.GetDirectories(targetDirectory)
Dim subdirectory As String
For Each subdirectory In subdirectoryEntries
DeleteFiles(subdirectory)
Next subdirectory
End Sub
Remember to import System.IO.
Points of Interest
As you can see, I wound up using both the Directory and DirectoryInfo classes as each one was easier to use in its place. Also, I am sure that there are millions
of other ways to accomplish the same thing, but one thing is for sure, it is anything but complicated. And that was my point.