none
Reading File Names in Folder aswell as in SubFolders. RRS feed

  • Question

  • Hi There.

    I use this code to give me a list of all MS Word Documents in a Folder.  

    I need some code that will not only give me the names in that Folder but the Names in all SubFolders aswell.  

    Can you please Help.  

    RecipeFolder = "M:\My Documents\My Resepte\Wenke & Boererate vir in en om die Huis\Huishoudelike Wenke"
    
    
    Sub FillRecipeListBox()
    
            Me.RecipeListBox.Items.Clear()
            Dim Files() As String = Directory.GetFiles(RecipeFolder)
            For Each file As String In Files
                RecipeListBox.Items.Add(Path.GetFileNameWithoutExtension(file))
            Next
    
        End Sub

    Monday, February 24, 2020 8:14 AM

Answers

  • Here is another method using .docx, if needed change to .doc

    Dim RecipeFolder = "M:\My Documents\My Resepte\Wenke & Boererate vir in en om die Huis\Huishoudelike Wenke"
    RecipeListBox.DataSource = Directory.GetFiles(RecipeFolder, "*.docx", SearchOption.AllDirectories).
        Select(Function(file) Path.GetFileNameWithoutExtension(file)).ToList


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Monday, February 24, 2020 12:00 PM
    Moderator

All replies

  • Hi,
    try following functions:

      Sub FillCategoryListBox()
    
        Dim RecipeFolder As String = "c:\temp" ' "M:\My Documents\My Resepte\Hoofgeregte\Drankies"
    
        CategoryListBox.DataSource = GetFilesRecursive(RecipeFolder).ToList
    
      End Sub
    
      Private Iterator Function GetFilesRecursive(folderPath As String) As IEnumerable(Of String)
        For Each filename In From f In Directory.GetDirectories(folderPath) Select Path.GetFileName(f)
          Yield filename
        Next
        For Each foldername In Directory.GetDirectories(folderPath)
          For Each filename In GetFilesRecursive(foldername)
            Yield filename
          Next
        Next
      End Function


    --
    Best Regards / Viele Grüße
    Peter Fleischer (former MVP for Developer Technologies)
    Homepage, Tipps, Tricks



    Monday, February 24, 2020 8:57 AM
  • Here is another method using .docx, if needed change to .doc

    Dim RecipeFolder = "M:\My Documents\My Resepte\Wenke & Boererate vir in en om die Huis\Huishoudelike Wenke"
    RecipeListBox.DataSource = Directory.GetFiles(RecipeFolder, "*.docx", SearchOption.AllDirectories).
        Select(Function(file) Path.GetFileNameWithoutExtension(file)).ToList


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Monday, February 24, 2020 12:00 PM
    Moderator