none
Loading Document Names RRS feed

  • Question

  • Hi ther.

    I am using this code below to populate a Listbox with Document Names, it works fine. 

    My problem is that in the folder ther is a Document called "TEMPLATE" that allso shows up in the list of Documents. 

    Is ther a way of preventing this of showing up.

     Dim Files() As String = Directory.GetFiles(RecipeFolder)
            For Each file As String In Files
                RecipeListBox.Items.Add(Path.GetFileName(file))


    Sunday, March 1, 2020 9:30 AM

Answers

  • You can use either this

    Dim Files() As String = Directory.GetFiles(RecipeFolder)
    
    For Each file As String In Files
        If Not Path.GetFileName(file).StartsWith("TEMPLATE") Then
            RecipeListBox.Items.Add(Path.GetFileName(file))
        End If
    Next

    Or

    Dim Files() As String = Directory.GetFiles(RecipeFolder)
    
    For Each file As String In Files
        If Not Path.GetFileName(file).Contains("TEMPLATE") Then
            RecipeListBox.Items.Add(Path.GetFileName(file))
        End If
    Next

    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



    Sunday, March 1, 2020 10:32 AM
    Moderator

All replies

  • Hi,
    you can use LinQ with Where like in following demo:

    Imports System.IO
    
    Public Class Form85
    
      Private RecipeListBox As New ListBox With {.Dock = DockStyle.Fill}
      Private Sub Form85_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.Controls.Add(RecipeListBox)
        Dim RecipeFolder = "c:\temp"
        RecipeListBox.DataSource = (From f In Directory.GetFiles(RecipeFolder)
                                    Let fn = Path.GetFileName(f)
                                    Where Not fn.StartsWith("temp") AndAlso Path.GetExtension(fn) = ".docx"
                                    Select fn).ToList
      End Sub
    
    End Class

    And to get count:

    Imports System.IO
    
    Public Class Form85
    
      Private RecipeListBox As New ListBox With {.Dock = DockStyle.Fill}
      Private Sub Form85_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.Controls.Add(RecipeListBox)
        Dim RecipeFolder = "c:\temp"
        Dim l = (From f In Directory.GetFiles(RecipeFolder)
                 Let fn = Path.GetFileName(f)
                 Where Not fn.StartsWith("temp") AndAlso Path.GetExtension(fn) = ".docx"
                 Select fn).ToList
        Dim fileCount As Integer = l.Count
        RecipeListBox.DataSource = l
      End Sub
    
    End Class


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


    Sunday, March 1, 2020 10:27 AM
  • You can use either this

    Dim Files() As String = Directory.GetFiles(RecipeFolder)
    
    For Each file As String In Files
        If Not Path.GetFileName(file).StartsWith("TEMPLATE") Then
            RecipeListBox.Items.Add(Path.GetFileName(file))
        End If
    Next

    Or

    Dim Files() As String = Directory.GetFiles(RecipeFolder)
    
    For Each file As String In Files
        If Not Path.GetFileName(file).Contains("TEMPLATE") Then
            RecipeListBox.Items.Add(Path.GetFileName(file))
        End If
    Next

    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



    Sunday, March 1, 2020 10:32 AM
    Moderator
  • You can use either this

    Dim Files() As String = Directory.GetFiles(RecipeFolder)
    
    For Each file As String In Files
        If Not Path.GetFileName(file).StartsWith("TEMPLATE") Then
            RecipeListBox.Items.Add(Path.GetFileName(file))
        End If
    Next

    Or

    Dim Files() As String = Directory.GetFiles(RecipeFolder)
    
    For Each file As String In Files
        If Not Path.GetFileName(file).Contains("TEMPLATE") Then
            RecipeListBox.Items.Add(Path.GetFileName(file))
        End If
    Next

    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



    Is it posable to count the files after loaded into the Listbox?

    Sunday, March 1, 2020 11:04 AM
  • The following provides a count.

    Dim countOfRecipes As Integer = RecipeListBox.Items.Count

    If you want this to be say placed into a label.

    Label1.Text = "Recipe/document count: " &  countOfRecipes.ToString()


    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


    Sunday, March 1, 2020 11:16 AM
    Moderator