Results 1 to 40 of 43

Thread: VB.Net: Recursive File Searching...

Threaded View

  1. #1

    Thread Starter
    Frenzied Member <ABX's Avatar
    Join Date
    Jul 2002
    Location
    Canada eh...
    Posts
    1,622

    VB.Net: Recursive File Searching...

    VB Code:
    1. Option Strict On
    2. Option Explicit On
    3.  
    4. Imports System.IO
    5.  
    6.     Public Class FileSearch
    7.  
    8.         Private Const DefaultFileMask As String = "*.*"
    9.         Private Const DefaultDirectoryMask As String = "*"
    10.  
    11. #Region " Member Variables "
    12.  
    13.         Private _InitialDirectory As DirectoryInfo
    14.         Private _DirectoryMask As String
    15.         Private _FileMask As String
    16.  
    17.         '
    18.         Private _Directories As New ArrayList
    19.         Private _Files As New ArrayList
    20.  
    21. #End Region
    22.  
    23. #Region " Properites "
    24.  
    25.         Public Property InitialDirectory() As DirectoryInfo
    26.             Get
    27.                 Return _InitialDirectory
    28.             End Get
    29.             Set(ByVal Value As DirectoryInfo)
    30.                 _InitialDirectory = Value
    31.             End Set
    32.         End Property
    33.         Public Property DirectoryMask() As String
    34.             Get
    35.                 Return _DirectoryMask
    36.             End Get
    37.             Set(ByVal Value As String)
    38.                 _DirectoryMask = Value
    39.             End Set
    40.         End Property
    41.         Public Property FileMask() As String
    42.             Get
    43.                 Return _FileMask
    44.             End Get
    45.             Set(ByVal Value As String)
    46.                 _FileMask = Value
    47.             End Set
    48.         End Property
    49.  
    50.         Public ReadOnly Property Directories() As ArrayList
    51.             Get
    52.                 Return _Directories
    53.             End Get
    54.         End Property
    55.         Public ReadOnly Property Files() As ArrayList
    56.             Get
    57.                 Return _Files
    58.             End Get
    59.         End Property
    60.  
    61.  
    62.  
    63. #End Region
    64.  
    65. #Region " Constructors "
    66.  
    67.         Public Sub New()
    68.  
    69.         End Sub
    70.  
    71.         Public Sub New( _
    72.             ByVal BaseDirectory As String, _
    73.             Optional ByVal FileMask As String = DefaultFileMask, _
    74.             Optional ByVal DirectoryMask As String = DefaultDirectoryMask)
    75.  
    76.             Me.New(New IO.DirectoryInfo(BaseDirectory), FileMask, DirectoryMask)
    77.  
    78.         End Sub
    79.  
    80.         Public Sub New( _
    81.             ByVal BaseDirectory As DirectoryInfo, _
    82.             Optional ByVal FileMask As String = DefaultFileMask, _
    83.             Optional ByVal DirectoryMask As String = DefaultDirectoryMask)
    84.  
    85.             _InitialDirectory = BaseDirectory
    86.             _FileMask = FileMask
    87.             _DirectoryMask = DirectoryMask
    88.  
    89.         End Sub
    90.  
    91. #End Region
    92.  
    93.         Protected Overrides Sub Finalize()
    94.             _Files = Nothing
    95.             _Directories = Nothing
    96.             MyBase.Finalize()
    97.         End Sub
    98.  
    99.  
    100.         Public Sub Search( _
    101.             Optional ByVal BaseDirectory As DirectoryInfo = Nothing, _
    102.             Optional ByVal FileMask As String = Nothing, _
    103.             Optional ByVal DirectoryMask As String = Nothing)
    104.  
    105.             If Not IsNothing(BaseDirectory) Then
    106.                 _InitialDirectory = BaseDirectory
    107.             End If
    108.  
    109.             If IsNothing(_InitialDirectory) Then
    110.                 Throw New ArgumentException("A Directory Must be specified!", "Directory")
    111.             End If
    112.  
    113.             If IsNothing(FileMask) Then
    114.                 _FileMask = DefaultFileMask
    115.             Else
    116.                 _FileMask = FileMask
    117.             End If
    118.  
    119.  
    120.             If IsNothing(DirectoryMask) Then
    121.                 _DirectoryMask = DefaultDirectoryMask
    122.             Else
    123.                 _DirectoryMask = DirectoryMask
    124.             End If
    125.  
    126.             DoSearch(_InitialDirectory)
    127.         End Sub
    128.  
    129.         Private Sub DoSearch(ByVal BaseDirectory As DirectoryInfo)
    130.  
    131.             Try
    132.                 _Files.AddRange(BaseDirectory.GetFiles(_FileMask))
    133.             Catch u As UnauthorizedAccessException
    134.                 'Siliently Ignore this error, there isnt any simple
    135.                 'way to avoid this error.
    136.  
    137.             End Try
    138.  
    139.             Try
    140.                 Dim Directories() As DirectoryInfo = BaseDirectory.GetDirectories(_DirectoryMask)
    141.                 _Directories.AddRange(Directories)
    142.  
    143.                 For Each di As DirectoryInfo In Directories
    144.                     DoSearch(di)
    145.                 Next
    146.  
    147.             Catch u As UnauthorizedAccessException
    148.                 'Siliently Ignore this error, there isnt any simple
    149.                 'way to avoid this error.
    150.  
    151.             End Try
    152.  
    153.         End Sub
    154.  
    155.     End Class

    Usage:

    VB Code:
    1. Dim x As New FileSearch(New IO.DirectoryInfo("d:\Media\"), "*.nfo")
    2.  
    3.         x.Search()
    4.  
    5.         MessageBox.Show(x.Files.Count) ' number of files that match "*.nfo" in D:\Media
    6.         MessageBox.Show(x.Directories.Count) ' the total number of directories looked through

    Edit: Now with MessageBox.Show
    Edit Again: Fixed some minor bugs, Masks were being overrided by Optional Parameters (duh), Added overload of the constructor, added some try... catch blocks to skip any directories/files that the user doesnt have access to.

    (It was buggy because I striped this out of a custom library (orginally, this was threaded)
    Last edited by <ABX; Jun 29th, 2005 at 10:25 PM.
    Tips:
    • Google is your friend! Search before posting!
    • Name your thread appropriately... "I Need Help" doesn't cut it!
    • Always post your code!!!! We can't read your mind!!! (well, at least most of us!)
    • Allways Include the Name and Line of the Exception (if one is occuring!)
    • If it is relevant state the version of Visual Studio/.Net Framwork you are using (2002/2003/2005)


    If you think I was helpful, rate my post
    IRC Contact: Rizon/xous ChakraNET/xous Freenode/xous

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width