65.9K
CodeProject is changing. Read more.
Home

Enhanced version of VB.NET GetFiles

starIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIconemptyStarIcon

1.87/5 (6 votes)

Nov 21, 2008

CPOL

1 min read

viewsIcon

25391

downloadIcon

220

This is an inline enumeration of files instead of building an array first.

Introduction

Part of my job requires batch processing large amounts of files, on the order of 100,000 to 1,000,000. Back in VB6 days, the code found each file in the directory. When upgrading to .NET, the GetFiles method was implemented and performance tanked. This is an implementation of the FindFirstFile, FindNextFile that can be called just like GetFiles with a bonus of returning all of the file attributes too.

Background

There are other versions of this out there, mostly in C# format, using the yield return which is available in VB.NET at this time.

This implementation handles multiple wildcard matches, but only one source directory.

It would be easy to add an Enumerator class around the Files class to handle multiple directories, but I did not have need for that at this time.

Using the Code

This is just a class module and a help file to explain the class. You simply add the class to your project and implement it just like the Microsoft version of GetFiles.

For Each file As Files.WIN32_FIND_DATA In _
    Files.GetFilesEx _
    ( _
        "c:\", _
        SYSTEM.FileIO.SearchOption.SearchAllSubDirectories, _
        "*.txt,*.doc" _
    )
    Console.WriteLine(file.ToString)
Next

This example will find all the text and document files on the C: drive.

Points of Interest

Unlike the .NET version of GetFiles, this version will look into any sub-directory and will not generate errors doing so.

History

  • Initial release: November 21, 2008.