Click here to Skip to main content
15,891,923 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a list of directories (121) that I want to generate all the files inside of them including sub directories. There are about 7300 files and folders inside the original list. EnumerateFiles and GetFiles take forever to populate the list. (8 minutes) I have done a comparison of a cmd line "Dir /s" and the "enumeratefiles" for one of the largest directories. "enumeratefiles" takes about 35 seconds, and "Dir /s" takes 2 seconds. I am just looking for a list of files and folders with their complete path name such as what you get from the cmd line "Dir /s". Is there a faster method for me to get this list? I have been trying the VB "Dir(filename)" but cannot seem to get this to work. The original list of files is coming from an array that I have previously populated. I'm new at this so I am probably not using good coding practices. :/

What I have tried:

For Each filename As String In IO.Directory.EnumerateFiles("o:\tapes\maxim500\dnc", "*", IO.SearchOption.AllDirectories)

If TextBox1.Text = "" Then
TextBox1.Text = filename
Else
TextBox1.Text = TextBox1.Text & vbNewLine & filename
End If


Next
Posted
Updated 9-Jan-18 2:57am

You are constantly concatenating strings, which can explain the poor performance you get.
You should use a StringBuilder to construct the string, and assign it to the text property at the end.
VB
Dim sb As new System.Text.StringBuilder(500000)
For Each filename As String In IO.Directory.EnumerateFiles("o:\tapes\maxim500\dnc", "*", IO.SearchOption.AllDirectories)
   sb.AppendLine(filename)
Next
TextBox1.Text = sb.ToString()

Hope this helps.
 
Share this answer
 
I am not sure why you want to display that much information in a textbox. Appending to a textbox is a very slow and resource intensive way to do it. You should use StringBuilder instead.

A better method, only slightly faster is to use a listbox. This also gives you the ability to select items.

VB
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

    Dim sb As StringBuilder = New StringBuilder()

    For Each filename As String In IO.Directory.EnumerateFiles("o:\tapes\maxim500\dnc", "*", IO.SearchOption.AllDirectories)
        sb.AppendLine(filename)
    Next

    TextBox1.Text = sb.ToString()

End Sub

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    ListBox1.DataSource = IO.Directory.EnumerateFiles("o:\tapes\maxim500\dnc", "*", IO.SearchOption.AllDirectories).ToList
End Sub


The fastest way to do it is to use a TreeView and lazy load on demand the folder that is expanded.
 
Share this answer
 
You are updating a text box by adding each file name. That consumes a lot of time because for each file name the text box content must be updated and redrawn.

You can append the names to a StringBuilder variable instead (which is faster than using a String) and assign that to the text box after all names has been determined:
VB
Dim listOfNames As New StringBuilder
For Each filename As String In IO.Directory.EnumerateFiles("o:\tapes\maxim500\dnc", "*", IO.SearchOption.AllDirectories)
    If listOfNames.Length > 0 Then
        listOfNames.AppendLine()
    End If
    listOfNames.Append(filename)
Next
TextBox1.Text = listOfNames.ToString()

If you want to have the text box updated periodically while enumerating, add a counter variable and update the text box when that crosses a boundary (can be done using the Mod operator).
 
Share this answer
 
As allready mentioned in the other Answers, that String concatenating and constant redrawing is your problem.
but why enumerate anyway, just to concat or append your items.

If you realy want to display it in a TextBox, this one liner should perform equaly fast.
VB
TextBox1.Text = String.Join(Environment.NewLine, IO.Directory.GetFiles("o:\tapes\maxim500\dnc", "*", IO.SearchOption.AllDirectories))
(I think thats a bad design decicion)


If you realy have like 7k entrys..
I would prefer something like Graeme_Grant mentioned, a TreeView opening only the needed subfolders, or a ListView in VirtualMode .. maybee both mixed, depending on how many files are in a single subfolder

working with that 7k line TextBox.. who the hell is gona do anyting with that ^^ like searching that one entry you need.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900