Page 1 of 2 12 LastLast
Results 1 to 40 of 43

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

  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

  2. #2
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    Re: VB.Net: Recursive File Searching...

    You might want to change the variable name "Directory" to something else. It conflicts with IO.Directory.
    I don't live here any more.

  3. #3
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: VB.Net: Recursive File Searching...

    What is the xous.Common. in the code. It worked without it.

    VB Code:
    1. Dim x As New xous.Common.FileSearch(New IO.DirectoryInfo("d:\Media\"), "*.nfo")

  4. #4

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

    Re: VB.Net: Recursive File Searching...

    Quote Originally Posted by dglienna
    What is the xous.Common. in the code. It worked without it.

    VB Code:
    1. Dim x As New xous.Common.FileSearch(New IO.DirectoryInfo("d:\Media\"), "*.nfo")
    heh... I ripped this code out of a library of mine... I removed the Namespace on the class but I guess I was still thinking it was there when I wrote the example.
    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

  5. #5
    New Member
    Join Date
    Jul 2005
    Posts
    2

    Re: VB.Net: Recursive File Searching...

    I was referred to your code from somebody on an other board and it has been an absolute life saver for me.

    Can you tell me if there is a simple change/addition that can be made that will allow this search app to also search the subfolders of the selected folder?

    Thanks
    Brad

  6. #6
    New Member
    Join Date
    Jul 2005
    Posts
    2

    Re: VB.Net: Recursive File Searching...

    Quote Originally Posted by bfackrell
    I was referred to your code from somebody on an other board and it has been an absolute life saver for me.

    Can you tell me if there is a simple change/addition that can be made that will allow this search app to also search the subfolders of the selected folder?

    Thanks
    Brad
    Sorry...I see that it does search subdirectories.

    Thanks agian.

  7. #7
    Member
    Join Date
    Jul 2005
    Posts
    36

    Re: VB.Net: Recursive File Searching...

    I want to use this to search for images. how can i alter this so it wil accept more then one file mask when it search?

    Thanks

  8. #8

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

    Re: VB.Net: Recursive File Searching...

    I would modify it like this:

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

    Sample Usage:

    VB Code:
    1. Dim x As New FileSearch()
    2.  
    3.         x.Search("d:\Media\", "*.jpg;*.jpeg")
    4.  
    5.         MessageBox.Show(x.Files.Count) ' number of files that match "*.jpg" and "*.jpeg" in D:\Media\Pictures
    6.         MessageBox.Show(x.Directories.Count) ' the total number of directories looked through
    Last edited by <ABX; Jul 20th, 2005 at 09:09 PM. Reason: Minior error overlooked in DoSearch()
    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

  9. #9
    Member
    Join Date
    Jul 2005
    Posts
    36

    Re: VB.Net: Recursive File Searching...

    Thanks for that.

    I tried it and go this error message
    An unhandled exception of type 'System.NullReferenceException' occurred in Send To DLAB.exe

    Additional information: Object reference not set to an instance of an object.

    at line:
    Mask = Mask.Trim(";"c)

  10. #10

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

    Re: VB.Net: Recursive File Searching...

    Argh... Fixed.

    Removed the Optional Parameters from the constructor and modified it so a more than one search can be preformed on an Instance.
    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

  11. #11
    New Member
    Join Date
    Aug 2005
    Posts
    5

    Red face Re: VB.Net: Recursive File Searching...

    Hi!
    This is an excelent function!
    Is there anyway I can get the full path to the files found?
    I tryed and tryed but I couldnt get it, I could get the current directory for the file but no the full path to the file.
    If anyone knows how to do it pleas lend me a hand.
    Thank you very much.

  12. #12
    Member
    Join Date
    Jul 2005
    Posts
    36

    Re: VB.Net: Recursive File Searching...

    The function returns an arraylist of fileinfo
    you can get the fullpath by :

    x.Search(selectedPath, "*.jpg;*.jpeg;*.tif;*.tiff")
    For Each imageFiles As FileInfo In x.Files
    Dim img = Image.FromFile(imageFiles.FullName)
    If img.PhysicalDimension.width < minWIdth Or img.PhysicalDimension.height < minHeight Then
    arAutoImageNotOK.Add(imageFiles)
    Else
    arAutoImageOK.Add(imageFiles)
    End If
    img = Nothing
    next


    This is just an example because I use it to check the dimension of the images which I used x to search for.

  13. #13
    New Member
    Join Date
    Aug 2005
    Posts
    5

    Re: VB.Net: Recursive File Searching...

    Thank you for the reply but I use this class for general search not only of images.
    Can the full path be retreived if its not an image?
    Tnx!

  14. #14
    Member
    Join Date
    Jul 2005
    Posts
    36

    Re: VB.Net: Recursive File Searching...

    Yes,

    It is an arraylist of fileinfo. so you will need to use for each statement to retrieve each element of the array. then use the element.fullname to get the full path

  15. #15
    New Member
    Join Date
    Aug 2005
    Posts
    5

    Re: VB.Net: Recursive File Searching...

    ahhhhhhhhhhh ok, it my first time using an arraylist in net, i'll try it!
    Thank you very much!!

  16. #16
    Member
    Join Date
    Jul 2005
    Posts
    36

    Re: VB.Net: Recursive File Searching...

    anytime

  17. #17
    New Member
    Join Date
    Nov 2005
    Posts
    1

    Re: VB.Net: Recursive File Searching...

    Hi there, ...great code,... but is there the possiblity to add multithreading to it so in GUI's users can work without having to wait for search-results?

  18. #18
    KrisSiegel.com Kasracer's Avatar
    Join Date
    Jul 2003
    Location
    USA, Maryland
    Posts
    4,985

    Re: VB.Net: Recursive File Searching...

    Quote Originally Posted by singletrail
    Hi there, ...great code,... but is there the possiblity to add multithreading to it so in GUI's users can work without having to wait for search-results?
    Just add it yourself. That way you can control GUI updating
    KrisSiegel.com - My Personal Website with my blog and portfolio
    Don't Forget to Rate Posts!

    Free Icons: FamFamFam, VBCorner, VBAccelerator
    Useful Links: System.Security.SecureString Managed DPAPI Overview Part 1 Managed DPAPI Overview Part 2 MSDN, MSDN2, Comparing the Timer Classes

  19. #19
    Lively Member
    Join Date
    Nov 2006
    Posts
    67

    Re: VB.Net: Recursive File Searching...

    First off, Hi!
    Second, super code dude.
    And so... I want to add the search result to a listbox called ListBox1
    How?

    This is what I've tried but it does not work.
    Dim x As New FileSearch()
    x.Search("e:\mp3\", "*.mp3")
    ListBox1.Items.Add(x)

    The listbox gets just one line which says:
    "WindowsApplication1.FileSearch"

    Thanks

  20. #20

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

    Re: VB.Net: Recursive File Searching...

    Change your code to the following

    VB Code:
    1. 'Creates an instance of FileSearch
    2. Dim x As New FileSearch()
    3.  
    4. 'Preforms the Search
    5. x.Search("e:\mp3\", "*.mp3")
    6.  
    7. ' Takes the results of the search x.Files, which is an array list
    8. ' And converts it to an Array to allow it to be passed to the AddRange method
    9.  
    10. ListBox1.Items.AddRange(x.Files.ToArray)

    If you want to have the results formatted differently, you must add each item Individually.

    VB Code:
    1. ' eg...
    2.  
    3. For each f as System.IO.FileInfo in x.Files
    4.     Listbox1.Items.Add(f.toString())
    5. Next
    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

  21. #21
    Lively Member
    Join Date
    Nov 2006
    Posts
    67

    Re: VB.Net: Recursive File Searching...

    Thanks man. That helped alot.

  22. #22
    New Member
    Join Date
    Sep 2007
    Posts
    2

    Re: VB.Net: Recursive File Searching...

    Hi,
    I am developing a text search tool which gives the user the ability to open and scan a set of text based source files for a particular text string. This class looks like a great candidate for the Job. However, I would like to give the user the ability to scan various file types in either one parent directory, or the parent directory & all its child directories.

    What is the correct usage of the class for finding files in just one directory, or a particular directory and all its child directories?

    Thanks in advance.

  23. #23
    New Member
    Join Date
    Dec 2007
    Posts
    1

    Re: VB.Net: Recursive File Searching...

    Quote Originally Posted by quacka
    Yes,

    It is an arraylist of fileinfo. so you will need to use for each statement to retrieve each element of the array. then use the element.fullname to get the full path
    Can you give a sample for this.

    Code:
            Dim i As Integer
            For i = 0 To x.Files.Count - 1
                Console.WriteLine(CStr(x.Files.Item(i)))
            Next
    I tried that but its not working.

  24. #24
    Member
    Join Date
    Feb 2008
    Posts
    44

    Re: VB.Net: Recursive File Searching...

    The class written by ABX is very well written but I if you are looking for a simpler solution as I was you may find this one-liner useful:

    Code:
     Dim searchResults As String() = Directory.GetFiles(SOFTWARE_RELEASE_FOLDER, "*.tgz", SearchOption.AllDirectories)
    where SOFTWARE_RELEASE_FOLDER is the base directory as a string, "*.tgz" is the file mask, and the third parameter tells the function to search recursively.

    The return is an array of strings which are the full path to each identified file.

  25. #25
    New Member
    Join Date
    Feb 2008
    Posts
    6

    Re: VB.Net: Recursive File Searching...

    Hi

    can i know how this one liner will be usefull to me? i mean i am a beginner in programming vb.net. i want to search a given file name and copy the file name in a specific folder. plz help me .


    thank you

  26. #26
    Member
    Join Date
    Feb 2008
    Posts
    44

    Re: VB.Net: Recursive File Searching...

    This would search for files ending in .txt in c:\ and any subdirectories. Then loops through the results and displays each in a message box. That help?

    Code:
    Dim searchResults As String() = Directory.GetFiles("c:\", "*.txt", SearchOption.AllDirectories)
    
    For each result as string in searchResults
    messagebox.show(result)
    next

    You can use system.IO.File.Copy(sourceFile, destinationFile) to copy files.

  27. #27
    New Member
    Join Date
    Feb 2008
    Posts
    6

    Re: VB.Net: Recursive File Searching...

    Thanx jwakeman

  28. #28
    New Member
    Join Date
    Feb 2008
    Posts
    6

    Re: VB.Net: Recursive File Searching...

    hi jakeman

    thanx for the help. only now i tried ur tip.
    i couldnot get the word "searchoption" so how can i search all directories.
    please help.

  29. #29
    New Member
    Join Date
    Feb 2008
    Posts
    6

    Re: VB.Net: Recursive File Searching...

    without the third parameter ie the searchoption, the code helps to search the files. but i want to search the subdirectories if it is not in the given source. i am using vb2003.
    please help.

    thanx

  30. #30
    New Member
    Join Date
    Apr 2008
    Posts
    1

    Lightbulb Re: VB.Net: Recursive File Searching...

    Hey man Awesome code! i have been looking for something like this for 6months!
    i was wondering how could you get it to show the currently scanned file in a
    little text label or something, and how to stop the search button. i need it not
    to frezz the program when searching too cos it shows the user nothing while it works untill you get a message with the things found.

    i love the code and you rock dude!

    Jared Woodruff

  31. #31
    New Member
    Join Date
    Sep 2008
    Posts
    1

    Re: VB.Net: Recursive File Searching...

    I'm using this like:
    x.Search(My.Computer.FileSystem.Drives.Item(0).ToString, "*.png;*.Gif;*.Jpg;*.Png;*.tif;*.jpeg", "Windows")
    So I want to get all the picture but not at the folder windows.
    But when I try this. I still get the picture from the folder Windows..and I cant figure out why...

  32. #32

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

    Re: VB.Net: Recursive File Searching...

    Quote Originally Posted by jwakeman
    The class written by ABX is very well written but I if you are looking for a simpler solution as I was you may find this one-liner useful:

    Code:
     Dim searchResults As String() = Directory.GetFiles(SOFTWARE_RELEASE_FOLDER, "*.tgz", SearchOption.AllDirectories)
    where SOFTWARE_RELEASE_FOLDER is the base directory as a string, "*.tgz" is the file mask, and the third parameter tells the function to search recursively.

    The return is an array of strings which are the full path to each identified file.
    The class I published above is deprecated in favor of the method mentioned above as Microsoft extended the Directory.GetFiles() method to include support for recursive file searching in the .Net Framework versions 3.5, 3.0, 2.0.

    The above code should only be used in previous versions of the .Net framework or for a real world example on how and why to use recursion.

    Quote Originally Posted by mxtech
    Hey man Awesome code! i have been looking for something like this for 6months!
    i was wondering how could you get it to show the currently scanned file in a
    little text label or something, and how to stop the search button. i need it not
    to frezz the program when searching too cos it shows the user nothing while it works untill you get a message with the things found.

    i love the code and you rock dude!

    Jared Woodruff
    This code is synchronous which means that it was not designed for the use you have in mind. You would need to pretty much rewrite the above code from scratch to make it asynchronous.
    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

  33. #33
    New Member
    Join Date
    Nov 2008
    Posts
    2

    Re: VB.Net: Recursive File Searching...

    Thanks! great code!
    But how can i make it write the results into .txt file?
    And that other guy asked about getting the full path, and i just dont get how to use it, Help please!
    Sorry for my english..
    Thanks again!

  34. #34
    Lively Member
    Join Date
    Nov 2006
    Posts
    67

    Re: VB.Net: Recursive File Searching...

    I'm no VB guru but I would do it like this.
    Code:
    Imports System.IO
    Public Class Form1
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
        End Sub
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim s As New FileSearch()
            Dim sw1 As New StreamWriter("test.txt", True)
            Dim sw2 As New StreamWriter("test2.txt", True)
            s.Search("c:\windows", "*.dll")
    
            For Each file As System.IO.FileInfo In s.Files
                sw1.WriteLine(file.ToString())
                sw2.WriteLine(file.FullName.ToString())
            Next
            End
        End Sub
    End Class
    This is with <ABX's modified version
    FORZA ROSSONERI! CAMPIONI!!!

  35. #35
    New Member
    Join Date
    Nov 2008
    Posts
    2

    Re: VB.Net: Recursive File Searching...

    Thanks!!

  36. #36
    New Member
    Join Date
    Apr 2007
    Posts
    5

    Re: VB.Net: Recursive File Searching...

    Thank you alot
    Have Nice Day Cya

  37. #37
    Fanatic Member sbasak's Avatar
    Join Date
    Aug 2001
    Location
    Globe Trotter
    Posts
    524

    Re: VB.Net: Recursive File Searching...

    This is a great piece of code!

    But can someone please tell me how to search for files in given directory only excluding any subdirectories under it?

    Thanks
    Life is a one way journey, not a destination. Travel it with a smile and never regret anything.
    Yesterday is history, tomorrow is a mystery, today is gift - that's why we call it present.

  38. #38
    Lively Member
    Join Date
    Nov 2006
    Posts
    67

    Re: VB.Net: Recursive File Searching...

    If you don't need to search subdirectories then this code will do just fine

    Code:
            Dim filetype As String = "*.txt"
            Dim dir As String = "c:\windows"
            For Each filename As String In System.IO.Directory.GetFiles(dir, filetype)
                ListBox1.Items.Add(filename) 'Or do whatever with the result
            Next
    FORZA ROSSONERI! CAMPIONI!!!

  39. #39
    New Member
    Join Date
    Mar 2009
    Posts
    1

    Re: VB.Net: Recursive File Searching...

    Lol, I signed up just so I could help sbasak out but you already beat me to it Rossonero

    ..but now I'm wondering, how would one accomplish the same thing using ABX's modifications. That is, not searching deeper then the directory specified ?

    anyone know ?

  40. #40
    Fanatic Member sbasak's Avatar
    Join Date
    Aug 2001
    Location
    Globe Trotter
    Posts
    524

    Re: VB.Net: Recursive File Searching...

    Yes, I was looking for a modification in original code.

    In the original Search function, I tried putting a 3rd parameter for absurd directory name like "QQQQQ" to look for only those directories (thus skipping normal sub directories) but it didn't work.

    eg. x.Search("F:\Windows", "*.doc;*.txt", "QQQZZZ")

    Also System.IO.Directory.GetFiles(dir, filetype) can't search for multiple wildchars as in ABX's function.

    GetFiles has a 3rd parameter which tells whether to search inside sub-directories as well. But it can't interpret multiple wildchars!
    Last edited by sbasak; Mar 10th, 2009 at 05:12 AM.
    Life is a one way journey, not a destination. Travel it with a smile and never regret anything.
    Yesterday is history, tomorrow is a mystery, today is gift - that's why we call it present.

Page 1 of 2 12 LastLast

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