Results 1 to 5 of 5

Thread: [Resolved][2008]searching subfolders

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Apr 2008
    Posts
    74

    [Resolved][2008]searching subfolders

    Well looks like 99% of the codes I found in the help file & in this forum are broken, they simply arent working. I get errors like file type not defined, or not declared.

    I know this is simple, but i just cant figure out how can i make it search further than 1 sub folder. I heard that this should be done recrusively, but I dont really understand how that thing works. Mayb someone can help me out hire?

    This is my code so far, that works it cant search Drives tho..
    Code:
           Dim startdir = "C:\New Folder\"
            For Each listfolders As String In My.Computer.FileSystem.GetDirectories(startdir)
    
                Me.ListBox1.Items.Add(listfolders)
    
                For Each listfiles As String In My.Computer.FileSystem.GetFiles(listfolders)
                    Me.ListBox1.Items.Add(listfiles)
                Next
    
            Next
    Last edited by goldenix; Apr 8th, 2008 at 05:35 PM.

    M.V.B. 2008 Express Edition

  2. #2
    Frenzied Member Icyculyr's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    1,934

    Re: [2008]searching subfolders

    Code:
    Dim sGetFiles() As String = IO.Directory.GetFiles("PATH", "*.*", SearchOption.AllDirectories)
    That retrieves all files the "*.*" part from, PATH, and searches all sub directories.. for files as well..

    Cheers

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Apr 2008
    Posts
    74

    Re: [2008]searching subfolders

    Quote Originally Posted by Icyculyr
    That retrieves all files the "*.*" part from, PATH, and searches all sub directories.. for files as well..
    Cheers
    this is strange Im getting: 'SearchOption' is not declared. error

    M.V.B. 2008 Express Edition

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: [2008]searching subfolders

    If you haven't imported the System.IO namespace then it would be IO.SearchOption.AllDirectories.

    Just keep in mind that if such a search encounters an inaccessible folder it will fail and throw an exception. For that reason you cannot search your entire C; drive that way as it will always fail. If you might be searching a folder tree with inaccessible folders then you need to write your own recursive file search. It's dead easy using Directory.GetFiles and Directory.GetDirectories and there would be more than one example on the forums already.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Apr 2008
    Posts
    74

    Re: [Resolved][2008]searching subfolders

    Thanx both of you.

    Hire is the code if someone has the same question
    Code:
         Dim file = "C:\New Folder"
         Dim sGetFiles() As String = IO.Directory.GetFiles(file, "*.*", IO.SearchOption.AllDirectories)
         For ii = 0 To sGetFiles.GetUpperBound(0)
             MsgBox(IO.Path.GetFileName(sGetFiles(ii)))
         Next

    M.V.B. 2008 Express Edition

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