dcsimg
Results 1 to 4 of 4

Thread: ListFiles - In all all folders and sub folders

  1. #1

    Thread Starter
    Addicted Member sinner0636's Avatar
    Join Date
    Sep 2009
    Posts
    233

    ListFiles - In all all folders and sub folders

    hello im trying to find the fastest way to get multiple paths and all there folder and sub folders any ideas how i can do this? thanks

    THIS WORKS
    Code:
    Dim RecentPath As String = (Environment.GetFolderPath(Environment.SpecialFolder.Recent))
    Try
    For Each FileName As String In IO.Directory.GetFiles(RecentPath, "*.*", IO.SearchOption.AllDirectories)
     ListView1.Items.Add(FileName )
    Next
     Catch ex As Exception
    'err
    End Try
    THIS DOES NOT
    Code:
     Dim RecentPath As String = (Environment.GetFolderPath(Environment.SpecialFolder.Recent))
     Dim CookiesPath As String = (Environment.GetFolderPath(Environment.SpecialFolder.Cookies))
    
    Dim SpecialFolders As String() = {CookiesPath , RecentPath }
    
    Try
    For Each Path As String In SpecialFolders
    For Each FileName As String In IO.Directory.GetFiles(Path, "*.*", IO.SearchOption.AllDirectories)
    ListView1.Items.Add(FileName)
    Next
    Next
    
    Catch ex As Exception
    'err
     End Try

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    100,054

    Re: ListFiles - In all all folders and sub folders

    The problem with that GetFiles method is that it doesn't allow you to ignore inaccessible folders and will simply throw an exception. If your root folder may contain a subfolder that is inaccessible then you simply can't use it. The alternative is to write your own recursive file search. There are many, MANY tutes and examples around of writing a recursive file search so I'm not going to provide yet another one. There's more than one even just ion our own CodeBank forum.
    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

  3. #3

    Thread Starter
    Addicted Member sinner0636's Avatar
    Join Date
    Sep 2009
    Posts
    233

    Re: ListFiles - In all all folders and sub folders

    true i thought u would be able to loop an array of paths that way with little code but ill prob just stick with stack of paths method

    SCAN FUNCTION
    Code:
        Public Function ScanSpecialFolderItems(ByVal FolderPath As String)
            Dim f As New List(Of String)
            Dim Stack As New Stack(Of String)
            Stack.Push(FolderPath)
    
            Do While (Stack.Count > 0)
                Try
                    Dim Path As String = Stack.Pop
                    f.AddRange(IO.Directory.GetFiles(Path, "*.*"))
                    For Each FolderName As String In IO.Directory.GetDirectories(Path)
                        Application.DoEvents()
                        Stack.Push(FolderName)
                    Next
                Catch ex As Exception
                    'err
                End Try
            Loop
            Return f
        End Function
    BUTTON CLICK
    Code:
            Dim RecentPath As String = (Environment.GetFolderPath(Environment.SpecialFolder.Recent))
            Dim CookiesPath As String = (Environment.GetFolderPath(Environment.SpecialFolder.Cookies))
    
            Dim SpecialFolders As String() = {CookiesPath, RecentPath}
            Try
                For Each SpecialPath In SpecialFolders
                    Dim list As List(Of String) = ScanSpecialFolderItems(SpecialPath)
                    For Each Item As String In list
                        Dim Items As String() = New String(0) {}
                        Dim Listitm As ListViewItem
                        Items(0) = Item
                        Listitm= New ListViewItem(Items, 0)
                        ListView1.Items.Add(Listitm)
                    Next
                Next
            Catch ex As Exception
                'err
            End Try
    Last edited by sinner0636; Dec 29th, 2017 at 08:17 AM.

  4. #4
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    100,054

    Re: ListFiles - In all all folders and sub folders

    Quote Originally Posted by sinner0636 View Post
    true i thought u would be able to loop an array of paths that way with little code
    You absolutely can do that, but only if you can get that array of paths in the first place, which you can't if there's an inaccessible folder under the root folder you specify.
    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
  •  



Featured


Click Here to Expand Forum to Full Width