• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 200
  • Last Modified:

Add Progress Bar to this code?

Is it possible to add a progress bar to this code? How would I do it?

It pretty much just loops through all the files in a directory and displays the name

Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
        GetDirectories("C:\Program Files")
    End Sub

    Sub GetDirectories(ByVal Directory As String)
        Dim di As New DirectoryInfo(Directory)

        For Each fi As FileInfo In di.GetFiles()
            lblStatus.Refresh()
            lblStatus.Text = fi.Name
        Next
        For Each dir As DirectoryInfo In di.GetDirectories()
            GetDirectories(dir.FullName)

        Next
    End Sub
0
JRockFL
Asked:
JRockFL
  • 3
1 Solution
 
S-TwilleyCommented:
Will there be any other processing added later?

If there is, it might be worth getting a file count (using similar code to what you have already)...

then using that file count and reusing your code with the processing part
0
 
JRockFLAuthor Commented:
I was just experimenting with some code, the little application that I was working on just deletes tmp files.

No real other processing.
0
 
S-TwilleyCommented:
If you have two progress bars, you could have one that shows the number of directories parsed... and the 2nd showing the current directory.

    Function GetDirectoryCount(ByVal Directory As String) AS Integer
        Dim di As New DirectoryInfo(Directory)
        Dim subDi As DirectoryInfo

        Dim dirCount as integer = 1 ' 1 for the directory itself    
 
        For Each subDi In di.GetDirectories()
            dirCount += GetDirectoryCount(dir.FullName)
        Next

        Return dirCount
    End Function

=======

    Dim totCount as Integer = 0
    Dim currDirIndex As Integer = 0
 
   Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
        totCount = GetDirectoryCount("C:\Program Files\")
        ProgressBar1.Value = 0
        ProgressBar1.Max = totCount
        currDirIndex = 0
        GetDirectories("C:\Program Files\")
    End Sub

    Sub GetDirectories(ByVal Directory As String)
        Dim di As New DirectoryInfo(Directory)
        currDirIndex += 1

        If curDirIndex <= ProgressBar1.Max Then
            ProgressBar1.Value = currDirIndex
        End If

        Dim fileList() as FileInfo = di.GetFiles()

        ProgressBar2.Value = 0
        ProgressBar2.Max = fileList.Length
     

        For Each fi As FileInfo In fileList
            If ProgressBar2.Value < ProgressBar2.Max Then ProgressBar2.Value += 1
            lblStatus.Refresh()
            lblStatus.Text = fi.Name
        Next

        For Each dir As DirectoryInfo In di.GetDirectories()
            GetDirectories(dir.FullName)
        Next
    End Sub
0
 
S-TwilleyCommented:
I wrote that straight into the post (not tested)... hopefully it's correct, but it still needs to loop thru initially to get a directory count.  Im gonna head off to bed now, so if you have probs, might be a while before i reply, but anothe expert may help
0
 
angelfeijooCommented:
JRockFL,

Most likely you won't find a simple way to show actual progress for your routine. So why don't you try to "guess" the progress. To do that you would need to guess the total number of files before you start the scan.

Let's use the registry to save the number of FILES you get every time you scan a path:

nLastNumberOfFiles = val(GetSetting("JRockFL", "ScanDirs", "Program Files"))

As the first time this will be 0, hardcode a initial number, like 36,620. (That's the number of files I have in my Program Files. Want to know how I got it?).

When drawing your progress bar just be carefull to cap the Value property so it doesn't go beyond 100% if your guess was below reality. If there were less files than expected, when you finish force the progress bar to "jump" to 100%.

Then save the total number of files actually found for later use:

SaveSetting("JRockFL", "ScanDirs", "Program Files", nTotalFilesFound)

Sorry I don't give you complete code, this is the idea. If you need assistance for drawing the progress bar just ask.

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now