Click here to Skip to main content
Licence CPOL
First Posted 21 Jun 2007
Views 8,909
Bookmarked 10 times

Deleting 0 length files

By | 21 Jun 2007 | Article
Code for deleting 0 length files.

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:

'targetDirectory is the starting point for our "directory walk"
Public Sub DeleteFiles(ByVal targetDirectory As String)

    'we create a new instance of DirectoryInfo 
    'to process our directories
    Dim di As New DirectoryInfo(targetDirectory)

    'the example in MS documentatation that I got this from
    '(http://msdn2.microsoft.com/en-us/library/07wt70x2.aspx)
    'is written for the Directory class, therefore it does not
    'use FileInfo class, but this way is much simpler (same thing 
    'could have been done I suppose with a combination of 
    'Directory and File classes
    Dim fileEntries As FileInfo() = di.GetFiles()

    ' Process the list of files found in the directory.
    Dim fileName As FileInfo

    For Each fileName In fileEntries
    'I wanted to get rid of 1K or smaller files, obviously any other 
    'criteria can be specified
        If fileName.Length <= 1024 Then
            Try
                fileName.Delete()
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
        End If
    Next fileName

    'processing ends here
    'this section does the recursion
    'for every subdirectory of the root, the sub calls itself until
    'there are no more subdirectories
    Dim subdirectoryEntries As String() = _
               Directory.GetDirectories(targetDirectory)

    ' Recurse into subdirectories of this directory.
    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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Mor DeRor



United States United States

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 1 PinmvpDave Kreskowiak9:46 14 Mar '10  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web03 | 2.5.120517.1 | Last Updated 21 Jun 2007
Article Copyright 2007 by Mor DeRor
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid