Snippet: CopyProgress

So you have written that new, must have program. Let us see it here.
Post Reply
Posts: 28
Joined: Friday 12th November 2021 1:31am
Location: North Central Florida

Snippet: CopyProgress

Post by Diverod » Thursday 20th January 2022 12:52am

I needed to copy large files to a Samba server and wanted a visual indication of the progress.
You will need ‘rsync’ installed if you don’t have it. (or just change to something else in the class and update the parsing)
You will need a ProgressBar and a TextLabel also if used as is.
It's not as smooth as I'd like but good enough for my needs.
I’m not good at ‘Class’ objects but tried my hand at it. (Thanks stevedee)
I want to see if I can get this to run in the background and found Cogier’s TaskExample and will attempt it later.
Haven’t done much error checking as it’s just a snippet.
' Gambas class file

Property Source As String         ' Source Path & File Name
Property Destination As String    ' Destination Path & File Name
Property StatusTXL As TextLabel   ' TextLabel on FMain to update
Property Progress As ProgressBar  ' ProgressBar on FMain to update

Private sSource As String
Private sDestination As String
Private cStatusTXL As TextLabel
Private cProgress As ProgressBar

Private Function Source_Read() As String          ' Retrieve Value
  Return sSource
Private Function Destination_Read() As String     ' Retrieve Value
  Return sDestination
Private Function StatusTXL_Read() As TextLabel    ' Retrieve Value
  Return cStatusTXL
Private Function Progress_Read() As ProgressBar   ' Retrieve Value
  Return cProgress

Private Sub Source_Write(Value As String)         ' Set Value
  sSource = Value
Private Sub Destination_Write(Value As String)    ' Set Value
  sDestination = Value
Private Sub StatusTXL_Write(Value As TextLabel)   ' Set Value
  cStatusTXL = Value
Private Sub Progress_Write(Value As ProgressBar)  ' Set Value
  cProgress = Value

Public Sub Run()
  Shell "rsync --progress " & sSource & " " & sDestination For Read As "Process"

Public Sub Process_Read()

  Dim sLine As String
  Dim saParts As String[]

  sLine = Read #Last, -2048
  sLine = Trim(sLine)                     ' Remove white spaces
  If InStr(sLine, "B/s") > 1 Then         ' Does it have Status Info?
    saParts = Split(sLine, " ", "", True) ' saParts = Bytes Copied | % Copied | Rate | Time Remaining
    cStatusTXL.Text = saParts[0] & " ● " & saParts[2] & " ● " & saParts[3] ' Update Status label
    cProgress.Value = Val(Replace(saParts[1], "%", "")) / 100               ' Update Progressbar
Usage Example:
' Gambas class file

Public Sub Form_Open()
  If Not System.Exist("rsync") Then
    Message.Error("You need to install the 'rsync' command for this example to work.", "OK")
  End If


Public CopyFile As New ClsCopyFile

Public Sub btnTest_Click()

  CopyFile.Source = txbSource.Text
  CopyFile.Destination = txbDestination.Text
  CopyFile.StatusTXL = txlCopyStatus
  CopyFile.Progress = pgbCopyFile

(14.83 KiB) Downloaded 54 times

Post Reply