Snippet: CopyProgress

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

Snippet: CopyProgress

Post by Diverod »

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.
Image
' 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
End
 
Private Function Destination_Read() As String     ' Retrieve Value
  Return sDestination
End
 
Private Function StatusTXL_Read() As TextLabel    ' Retrieve Value
  Return cStatusTXL
End
  
Private Function Progress_Read() As ProgressBar   ' Retrieve Value
  Return cProgress
End

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

Public Sub Run()
  
  ' https://linux.die.net/man/1/rsync
  Shell "rsync --progress " & sSource & " " & sDestination For Read As "Process"
  
End

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
  Endif
  
End
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")
    Me.Close
  End If

End

Public CopyFile As New ClsCopyFile

Public Sub btnTest_Click()

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

End
Attachments
CopyProgress.tar.gz
(14.83 KiB) Downloaded 436 times
Post Reply