none
how Search for a folder inside a folder by name in all partitions vb.net RRS feed

All replies

  • Based on this answer i did some 'tuning' to the code. this should basically do your desired work:

            public void Foo()
            {
                var searchFor = "temp";
                var driveList = (from d in DriveInfo.GetDrives() where d.DriveType == DriveType.Fixed select d.Name).ToList();
    
                var fileList = new List<String>();
                foreach (var item in driveList)
                {
                    GetAllDirs(item, fileList, searchFor);
                }
            }
    
            static void GetAllDirs(String path, IList<String> files, String searchFor)
            {
                try
                {
                    Directory.GetDirectories(path).Where(i => i.EndsWith($@"\{searchFor}")).ToList().ForEach(files.Add);
                    Directory.GetDirectories(path).ToList().ForEach(f => GetAllDirs(f, files, searchFor));
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                }
            }

    First of all get all drives in your system and scan each drive recursively for folders with the desired name ("Temp" in my case). Since Directory.GetDirectories(path) return a List with full pahname I added a Backslash to the seached directory name so no directories ending with (in my case) "Temp" will be included in the result.

    Please note that the search is case sensitive.

    Sorry, I did Oversee the VB.NET context... the code below should do the same:

        Public Sub Foo()
            Dim searchFor As String = "temp"
            Dim driveList As List(Of String) = (From d In DriveInfo.GetDrives() Where d.DriveType == DriveType.Fixed Select d.Name).List(Of String)
            Dim fileList As List(Of String) = New List(Of String)
    
            For Each item As String In driveList
                GetAllDirs(item, fileList, searchFor)
            Next
        End Sub
    
        Private Sub GetAllDirs(path As String, files As List(Of String), searchFor As String)
            Try
                For Each dPath As String In Directory.GetDirectories(path)
                    If dPath.EndsWith("\" + searchFor) Then
                        files.Add(dPath)
                    End If
                Next
                For Each dPath As String In Directory.GetDirectories(path)
                    GetAllDirs(dPath, files, searchFor)
                Next
            Catch e As Exception
                Console.WriteLine(e)
            End Try
        End Sub
    



    Monday, August 5, 2019 6:19 AM
  • Check the next example too:

    Dim main_folder = "C:\MyFolder1"

    Dim folder_to_find = "MyFolder2"

     

    Dim di = New DirectoryInfo(main_folder)

    Dim query = di.EnumerateDirectories(folder_to_find, SearchOption.AllDirectories)

     

    For Each r In query

     

        Console.WriteLine(r.FullName)

     

    Next

     

    Monday, August 5, 2019 7:00 AM