Click here to Skip to main content
15,880,796 members
Articles / Programming Languages / Visual Basic

Enhanced version of VB.NET GetFiles

Rate me:
Please Sign up or sign in to vote.
1.87/5 (6 votes)
21 Nov 2008CPOL1 min read 24.9K   220   10   5
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.

VB
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.

License

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


Written By
Software Developer (Senior) PRGX
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionGreat job Pin
N. Henrik Lauridsen3-Jul-13 1:50
N. Henrik Lauridsen3-Jul-13 1:50 
Simple and easy to use. Thank you for sharing.
QuestionAhum? Pin
Zaegra14-May-09 9:09
Zaegra14-May-09 9:09 
AnswerRe: Ahum? Pin
LeeBear3-Jul-13 3:38
LeeBear3-Jul-13 3:38 
GeneralMy vote of 1 Pin
ProJester124-Nov-08 11:09
ProJester124-Nov-08 11:09 
GeneralMy vote of 1 Pin
AxelM23-Nov-08 20:51
AxelM23-Nov-08 20:51 

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

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