Hi Dw

I've tried to update the progressbar under the For Each loop and it doesn't change its value, there was a piece of code I tried but can't remember now and that piece only updated let's say only once to (I think that value was 15 or 20). What I did was that I decide to first get the total number of files so that I can be able to update the progressbar for each file and I divided the results by 100 which gives me what value a progressbar should add for each file to the end, what I'm trying to do is also display or show the progress of a scan using progressbar.

Anyone maybe with a better way and most importantly how to update a progressbar value dynamically.

Recommended Answers

All 3 Replies

Hi

If I understand correctly, you want to do some work on files in a directory and provide feedback via a progress bar. If that is correct, hopefully the following will help.

First, add a progress bar and set its Step property to 1. This tells the progress bar how much to increment by between its minimum and maximum values. You don't need to do any calculations yourself.

The code below should be self explanatory, but basically, it gets the number of files in a directory for a particular pattern (such as text files) and then in this example, opens each file, reads the file line by line and outputs the contents to the output window and then updates the progress bar once each file has been processed using the PerformStep method.

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

    'Get number of files in source directory
    ScanDirectory("C:\Temp\", "*.txt")

End Sub

Private Sub ScanDirectory(ByVal sourceDirectory As String, ByVal filePattern As String)

    'Get number of files that match pattern in order to update progress bar maximum value
    scanProgress.Maximum = Directory.GetFiles(sourceDirectory, filePattern).Count - 1

    For Each currentFile As String In Directory.GetFiles(sourceDirectory, filePattern)

        For Each line As String In File.ReadLines(currentFile)
            Console.WriteLine(line)
        Next

        scanProgress.PerformStep()

    Next

End Sub

HTH

This example will count all files in C directory. You can change this to any directory you want. This should work for all types of files :

 Dim fileCount, filesWorkedOn as Integer

 filesWorkedOn = 0

 Dim di As New DirectoryInfo("c:\")
    ' Get a reference to each file in that directory..
 Dim fiArr As FileInfo() = di.GetFiles()

 fileCount = fiArr.Length ' file count in directory

 ProgressBar1.Value = 0 ' initially...

 Dim fri As FileInfo
 For Each fri In fiArr
   ' Perform any action on the file (using fri.Name)
   filesWorkedOn = filesWorkedOn + 1
   ProgressBar1.Value = filesWorkedOn / fileCount * 100 ' incrementing based on progress
 Next fri
Dim archivos As DirectoryInfo = New System.IO.DirectoryInfo(pictures_dir)

        ' tipos de archivos a subir
        Dim Result1 As FileInfo() = archivos.GetFiles("*.pdf")
        Dim Result2 As FileInfo() = archivos.GetFiles("*.txt")
        Dim Result3 As FileInfo() = archivos.GetFiles("*.ini")

        Dim narchivos As FileInfo() = New FileInfo(Result1.Length + Result2.Length + (Result3.Length - 1)) {}

        ' concatena archivos en narchivos
        Result1.CopyTo(narchivos, 0)
        Result2.CopyTo(narchivos, Result1.Length)
        Result3.CopyTo(narchivos, (Result2.Length + Result1.Length))


        EscanearOperaciones.lblProgreso.Text = "Subiendo " & "( " & narchivos.Length & " )" & " Archivos a FTP..."

        EscanearOperaciones.progressBarFile.Visible = True
        EscanearOperaciones.progressBarFile.Maximum = 1

        EscanearOperaciones.progressBarGlobal.Visible = True
        EscanearOperaciones.progressBarGlobal.Maximum = narchivos.Length

        For fn As Integer = 0 To narchivos.Length - 1

            Dim nombre As String = Path.GetFileName(narchivos(fn).FullName)
            Dim arch_completo As String = narchivos(fn).FullName

            Try
                upload(arch_completo, nombre)
            Catch

            End Try
        Next





        Private Sub upload(sourceFile As String, targetFile As String)

        Dim filename As String = "ftp://" & ftpServerIP & "//" & targetFile
        Dim ftpReq As FtpWebRequest = DirectCast(WebRequest.Create(filename), FtpWebRequest)

        ftpReq.UseBinary = True
        ftpReq.Method = WebRequestMethods.Ftp.UploadFile
        ftpReq.Credentials = New NetworkCredential(ftpUserID, ftpPassword)

        Dim b As Byte() = File.ReadAllBytes(sourceFile)

        ftpReq.ContentLength = b.Length
        Using s As Stream = ftpReq.GetRequestStream()
            s.Write(b, 0, b.Length)
        End Using

        Dim ftpResp As FtpWebResponse = DirectCast(ftpReq.GetResponse(), FtpWebResponse)

        If ftpResp IsNot Nothing Then
            EscanearOperaciones.progressBarFile.Increment(1)
            'MessageBox.Show(ftpResp.StatusDescription)
            'MessageBox.Show("Archivos Subidos Correctamente al FTP")
            EscanearOperaciones.progressBarGlobal.Increment(1)
        Else
            EscanearOperaciones.progressBarGlobal.Visible = False
            EscanearOperaciones.progressBarGlobal.Value = 0
        End If

    End Sub
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.