Results 1 to 8 of 8

Thread: [RESOLVED] [2005] Why isn't this searching the root folder?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    May 2006
    Location
    Manchester, England
    Posts
    255

    Resolved [RESOLVED] [2005] Why isn't this searching the root folder?

    Hi,
    Can anybody tell me why this bit of code is ignoring the root folder?

    Code:
        Private Function GetAllFolders(ByVal folder As String) As String()
    
            For Each strFolder As String In Directory.GetDirectories(folder, "*", SearchOption.TopDirectoryOnly)
    
                Try
    
                    strAllFolders.AddRange(GetAllFolders(strFolder))
    
                    SearchIn(strFolder)
    
                Catch ex As Exception
    
                    ' Ignore
    
                End Try
    
            Next
    
            Return strAllFolders.ToArray
    
        End Function
    At home - VB.NET 2005/2008 Express, Visual Web Developer 2005 Express
    At work - VS 2008 Standard (VB)
    .NET 2.0/3.5


    Visual Studio Express Learning Centre | How do I videos | MSDN VB Express Forum | MSDN VB Developer Centre

  2. #2
    Hyperactive Member
    Join Date
    Mar 2005
    Posts
    499

    Re: [2005] Why isn't this searching the root folder?

    You are searching within the root directory so your loop wont return the root folder itself.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    May 2006
    Location
    Manchester, England
    Posts
    255

    Re: [2005] Why isn't this searching the root folder?

    Is there a way to make it return the root folder?
    At home - VB.NET 2005/2008 Express, Visual Web Developer 2005 Express
    At work - VS 2008 Standard (VB)
    .NET 2.0/3.5


    Visual Studio Express Learning Centre | How do I videos | MSDN VB Express Forum | MSDN VB Developer Centre

  4. #4
    Frenzied Member stimbo's Avatar
    Join Date
    Jun 2006
    Location
    UK
    Posts
    1,739

    Re: [2005] Why isn't this searching the root folder?

    isn't that just the 'folder' variable, which you would obviously already know having passed the root path in the first place?
    Last edited by stimbo; Oct 31st, 2007 at 08:55 AM.
    Stim

    Free VB.NET Book Chapter
    Visual Basic 2005 Cookbook Sample Chapter

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    May 2006
    Location
    Manchester, England
    Posts
    255

    Re: [2005] Why isn't this searching the root folder?

    Yes it is, but I couldn't get it to even think about using 'folder'.

    To solve it, I've had to break it up a bit by creating a boolean value to MAKE it use 'folder', and once it has been used, change the boolean so that it processes strFolder.

    Not nice, but its the only way I could think of getting it to work.
    At home - VB.NET 2005/2008 Express, Visual Web Developer 2005 Express
    At work - VS 2008 Standard (VB)
    .NET 2.0/3.5


    Visual Studio Express Learning Centre | How do I videos | MSDN VB Express Forum | MSDN VB Developer Centre

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

    Re: [2005] Why isn't this searching the root folder?

    So what you're saying is that you want the root folder included in the array returned by the method, correct?
    vb.net Code:
    1. Private Function GetFolders(ByVal folder As String) As String()
    2.     Dim folders As New List(Of String)
    3.  
    4.     folders.Add(folder)
    5.  
    6.     Try
    7.         For Each subfolder As String In IO.Directory.GetDirectories(folder)
    8.             folders.AddRange(Me.GetFolders(subfolder))
    9.         Next subfolder
    10.     Catch
    11.         'This folder is inaccessible so ignore it and move on.
    12.     End Try
    13.  
    14.     Return folders.ToArray()
    15. End Function
    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

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    May 2006
    Location
    Manchester, England
    Posts
    255

    Re: [2005] Why isn't this searching the root folder?

    Thanks. This works - although it is practically identical to how my code originally looked.

    Can't claim that I fully understand it at this stage.
    At home - VB.NET 2005/2008 Express, Visual Web Developer 2005 Express
    At work - VS 2008 Standard (VB)
    .NET 2.0/3.5


    Visual Studio Express Learning Centre | How do I videos | MSDN VB Express Forum | MSDN VB Developer Centre

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

    Re: [RESOLVED] [2005] Why isn't this searching the root folder?

    This is what your code was doing:

    1. Add all the subfolders of the specified to the list.
    2. Make a recursive call for each subfolder.

    That's why the top-level folder was never added: because the first call to the method ignored the specified folder and started by adding the subfolders. This is what my code is doing:

    1. Add the specified folder to the list.
    2. Make a recursive call for each subfolder.

    Thus my code will start by adding the top-level folder to the list, which is exactly what you want. In my code each folder will be added to the list one recursive call later than it will in yours.
    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

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