hello guys. i got a method that someone showed here BUT it doesnt work 100%... i mean..i put this folder C:\Users\chris\Documents .. if i search for *.docx, it finds only the docx files that are in SUBFOLDERS inside the Documents.. it does NOT find docx files that are in C:\Users\chris\Documents . i tried C:\Users\chris\Documents C:\Users\chris\Documents\ C:\Users\chris\Documents\\ but nothing works(without \ with 1\ and with double\\). then i tried other folders and i see that it doesnt find the files that are in the "root" (for example C:\Users\chris). if i look to C:\Users\chris it will find files that are in SUBFOLDERS inside the chris but not the files that are in chris without subfolders

Private Sub getMyCoolFiles(ByVal selectedDirectoryToSearch As String, ByVal ListBoxForFoundFiles As ListBox)
        ListBox1.Items.Clear()
        Dim myCoolFolder As New IO.DirectoryInfo(selectedDirectoryToSearch)
        For Each foundDirectory In myCoolFolder.GetDirectories '// loop thru all top directories.
            Try
                '// search top directory and subfolders.
                For Each myFoundFile As IO.FileInfo In foundDirectory.GetFiles("*.html", IO.SearchOption.AllDirectories)
                    ListBox1.Items.Add(myFoundFile.FullName) '// add File to ListBox.
                Next
            Catch ex As UnauthorizedAccessException
                MsgBox(ex.Message) '// display Folders that have been Denied accessed to.
            End Try
        Next
        'MsgBox("Total Files: " & CInt(ListBoxForFoundFiles.Items.Count).ToString("#,###,###")) '// display Total File Count.
    End Sub

it looks like you are only search the sub directories..

change you code to the following and it will work..(i have tested it)

Private Sub getMyCoolFiles(ByVal selectedDirectoryToSearch As String, ByVal ListBoxForFoundFiles As ListBox)
        ListBox1.Items.Clear()
        Dim myCoolFolder As New IO.DirectoryInfo(selectedDirectoryToSearch)

        'Change
        For Each myfile As IO.FileInfo In myCoolFolder.GetFiles("*.txt", IO.SearchOption.TopDirectoryOnly)  'Notice the Options
            ListBox1.Items.Add(myfile.FullName)
        Next
        'Change End

        For Each foundDirectory In myCoolFolder.GetDirectories '// loop thru all top directories.
            Try
                '// search top directory and subfolders.
                For Each myFoundFile As IO.FileInfo In foundDirectory.GetFiles("*.txt", IO.SearchOption.AllDirectories)
                    ListBox1.Items.Add(myFoundFile.FullName) '// add File to ListBox.
                Next
            Catch ex As UnauthorizedAccessException
                MsgBox(ex.Message) '// display Folders that have been Denied accessed to.
            End Try
        Next
        'MsgBox("Total Files: " & CInt(ListBoxForFoundFiles.Items.Count).ToString("#,###,###")) '// display Total File Count.
    End Sub
commented: agree +13

hello.thank you very much.but i still have some problems.. if i put getMyCoolFiles("C:\Users\chris\Documents", ListBox1) it will find files in documents and in other folders too(subfolders)..the problem is that if i put
getMyCoolFiles("C:\", ListBox1) it will find only files in c or in any other subfolder like c:\new folder. it doesnt find files which are in documents..it should work because documents are in c:\users\chris\documents . so its like a subfolder in c.. thank you

anyone?

Member Avatar for Unhnd_Exception

Not quite sure what your wanting. It seems all the examples givin would work.

Heres another example.

This will retrieve every file in every subfolder in any directory including the directory specified. If this still doesn't help then.????????

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
       
    Dim FileNames As String() = GetDirectoryFileNames(My.Computer.FileSystem.SpecialDirectories.MyDocuments)
    ListBox1.Items.AddRange(FileNames)
End Sub

''' <summary>
'''Gets the names of all files in the specified directory including sub directories.
''' </summary>
''' <param name="DirectoryPath">The path of the directory to get the filenames from</param>
''' <remarks></remarks>
Private Function GetDirectoryFileNames(ByVal directoryPath As String) As String()

    Dim FileNames() As String = New String() {}
    Dim TempNames() As String

    Try
        For Each FileName As String In System.IO.Directory.GetFiles(directoryPath, "*.bmp")
            ReDim Preserve FileNames(FileNames.GetLength(0))
            FileNames(UBound(FileNames)) = FileName
        Next

        For Each Directory As String In System.IO.Directory.GetDirectories(directoryPath)
            TempNames = GetDirectoryFileNames(Directory)
            ReDim Preserve FileNames(FileNames.GetLength(0) + TempNames.GetLength(0) - 1)
            Array.Copy(TempNames, 0, FileNames, FileNames.GetLength(0) - TempNames.GetLength(0), TempNames.GetLength(0))
        Next

    Catch ex As Exception

    End Try

    Return FileNames

End Function

i will explain WHY the previous codes doesnt work good to me.
i have these paths c:\users\chris\documents and c:\ . lets say i have a file called 1.txt to the documents and 2.txt to c:\

IF i try to seach to c:\users\chris\documents it will find the 1.txt . IF i try to search to c:\ it will find ONLY the 2.txt

IT SHOULD FIND THE 1.txt TOO BECAUSE ITS IN C:\ too!! i mean c:\users\chris\documents is in c:\ ! thank you

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.