CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Feb 2000
    Location
    Belgium
    Posts
    2

    Searching subdirectories

    I'm writing a program to search a disk for files of a specified type (eg. *.bmp). I know how to search for the file in a directory I specify but i don't know how I can let the program also search all subdirectories. Is there anyone who can help me with this problem?

    Bert


  2. #2
    Join Date
    Jan 2000
    Location
    MO, USA
    Posts
    1,506

    Re: Searching subdirectories

    Set a reference to the Microsoft Scripting Runtime and then use code similar to this to loop through all the subdirectories... Place a command button on a form and then paste this code:

    option Explicit
    Dim fso as Scripting.FileSystemObject
    Dim ofolder as Scripting.folder
    Dim subflder as Scripting.folder
    Dim ofile as Scripting.File

    private Sub Command1_Click()

    set fso = new Scripting.FileSystemObject
    set ofolder = fso.GetFolder("C:\")

    me.Caption = "Searching... C:"

    for Each subflder In ofolder.SubFolders
    Call RecursiveSearch(subflder)
    next

    End Sub

    Sub RecursiveSearch(folder as Scripting.folder)
    Dim oFldr as Scripting.folder

    me.Caption = "Searching..." & folder.Path

    for Each oFldr In folder.SubFolders

    If oFldr.SubFolders.Count then
    RecursiveSearch oFldr
    End If

    for Each ofile In oFldr.Files
    If Right(ofile.Name, 4) = ".bmp" then
    MsgBox ofile.Name
    End If
    next

    next

    End Sub




    this code will go through the entire drive and popup a message box with the name of every .bmp file. The searching routine probably isn't the best, but it does get you through every directory and every subdirectory.

    Hope this helps,
    John


    John Pirkey
    MCSD
    www.ShallowWaterSystems.com
    John Pirkey
    MCSD (VB6)
    http://www.stlvbug.org

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

Featured