Add text in recursively copy

s1ckOh

Well-known member
Joined
Aug 1, 2011
Messages
68
Location
San Diego, Ca
Programming Experience
Beginner
I noticed that too about recursively getting files but just to be sure I added a delay after each file processed to see if its passed to a listbox and a label. turns out they were being passed, only too quickly for me to notice. Im just gunna paste what I did as an example. Hope it helps.

VB.NET:
Imports System.IO

Public Class Form2
    Dim array As New ArrayList

    Sub Delay(ByVal dblSecs As Double)
        Const OneSec As Double = 1.0# / (1440.0# * 60.0#)
        Dim dblWaitTil As Date
        Now.AddSeconds(OneSec)
        dblWaitTil = Now.AddSeconds(OneSec).AddSeconds(dblSecs)
        Do Until Now > dblWaitTil
            Application.DoEvents() ' Allow windows messages to be processed
        Loop
    End Sub

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        GetFilesRecursive("c:\users\home\desktop")

        For Each item As String In array
            Label1.Text = item
            ListBox1.Items.Add(item)
            Delay(1)
        Next
    End Sub

    Function GetFilesRecursive(ByVal initial As String) As List(Of String)
        Dim stack As New Stack(Of String)
        stack.Push(initial)
        Do While (stack.Count > 0)
            Dim dir As String = stack.Pop
            Try
                array.AddRange(Directory.GetFiles(dir, "*.*"))
                Dim directoryName As String
                For Each directoryName In Directory.GetDirectories(dir)
                    stack.Push(directoryName)
                Next
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
        Loop
        Return Nothing
    End Function
End Class
 
Last edited:

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,717
Location
Sydney, Australia
Programming Experience
10+
I created a label and code like

label1.text = sFileInfo.fullname (added it in "for each" loop)

but this code never fire until last file.

How to fix it?

The code does get executed. It's just that the UI doesn't get repainted because the UI thread is too busy doing the file copying. Ideally you should be doing the file copy in a secondary thread. You might use a BackgroundWorker and copy the files in its DoWork event handler and use the ReportProgress method and ProgressChanged event to update the UI.

If you want to stick to one thread, which is simpler but not really ideal, then you simply need to force a repaint of the Label after each change to its Text. You can do that by calling its Update method or, to ensure that the whole form gets repainted if required, call Application.DoEvents.
 

aspfun

Active member
Joined
Feb 16, 2010
Messages
34
Programming Experience
5-10
Thank you for help.
Once I added delay sub, it works now.
One more question:
How to add code to sort source by file name?
 
Last edited:

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,717
Location
Sydney, Australia
Programming Experience
10+
Thank you for help.
Once I added delay sub, it works now.
That is the worst possible solution I'm afraid.
One more question:
How to add code to sort source by file name?
That has nothing to do with the topic of this thread. One topic per thread and one thread per topic please. If you want to ask a question on a new topic then start a new thread with a title that describes the new topic.
 

s1ckOh

Well-known member
Joined
Aug 1, 2011
Messages
68
Location
San Diego, Ca
Programming Experience
Beginner
That is the worst possible solution I'm afraid.

Agreed, I tried the .Update and it worked way better. Thank you, I didn't know you could force it to update.

Changed...

VB.NET:
        For Each item As String In array
            Label1.Text = item
            Label1.Update()
            ListBox1.Items.Add(item)
            ListBox1.Update()
        Next

Removed the delay sub and works great.
 
Top Bottom