Skip to content
Darren Rose edited this page Jan 24, 2022 · 7 revisions

This section contains various VB.NET code snippets that may be useful to anyone creating apps which use RoboSharp

Basic Usage

Public Sub Backup()
	Dim backup As New RoboCommand()
	' events
	AddHandler backup.OnFileProcessed, AddressOf backup_OnFileProcessed
	AddHandler backup.OnCommandCompleted, AddressOf backup_OnCommandCompleted

	' copy options
	backup.CopyOptions.Source = Source.Text
	backup.CopyOptions.Destination = Destination.Text
	backup.CopyOptions.CopySubdirectories = True
	backup.CopyOptions.UseUnbufferedIo = True

	' select options
	backup.SelectionOptions.OnlyCopyArchiveFilesAndResetArchiveFlag = True
        backup.SelectionOptions.ExcludedFiles.Add("myfile.txt")
        backup.SelectionOptions.ExcludedFiles.Add("my file.txt")
        ' or
        'Dim FilestoExclude As New List(Of String)({"myfile.txt", "my file.txt"})
        'backup.SelectionOptions.ExcludedFiles.AddRange(FilestoExclude)
        'same methods can be used for ExcludedDirectories

	' retry options
	backup.RetryOptions.RetryCount = 1
	backup.RetryOptions.RetryWaitTime = 2
	backup.Start()
End Sub

Private Sub backup_OnFileProcessed(ByVal sender As Object, ByVal e As FileProcessedEventArgs)
	Me.BeginInvoke(CType(Sub()
		CurrentOperation.Text = e.ProcessedFile.FileClass
		CurrentFile.Text = e.ProcessedFile.Name
		CurrentSize.Text = e.ProcessedFile.Size.ToString()
	End Sub, Action))
End Sub

Private Sub backup_OnCommandCompleted(ByVal sender As Object, ByVal e As RoboCommandCompletedEventArgs)
	Me.BeginInvoke(CType(Sub()
		MessageBox.Show("Backup Complete!")
	End Sub, Action))
End Sub

RoboCopyResultsList

PR #127 adds some great new methods for handling results which can be used when running multiple jobs. This then allows you to view combined totals for files, directories and bytes as well as averages for speed, along with log, status and exit code information for each job

Basic usage example shown below - the new methods are all demonstrated in more detail in the included example BackupApp

Private JobResults As New Results.RoboCopyResultsList()

Private Sub copy_OnCommandCompleted(ByVal sender As Object, ByVal e As RoboCommandCompletedEventArgs)
   Me.BeginInvoke(CType(Sub()
	   OptionsGrid.IsEnabled = True
	   ProgressGrid.IsEnabled = False

	   Dim results = e.Results
	   JobResults.Add(e.Results)
   End Sub, Action))
End Sub

Private result As Results.RoboCopyResults = CType(Me.ListBox_JobResults.SelectedItem, Results.RoboCopyResults)
Private NL As String = Environment.NewLine
lbl_SelectedItemTotals.Content = $"Selected Job:" & $"{NL}Source: {If(result?.Source, "")}" & $"{NL}Destination: {If(result?.Destination, "")}" & $"{NL}Total Directories: {If(result?.DirectoriesStatistic?.Total, 0)}" & $"{NL}Total Files: {If(result?.FilesStatistic?.Total, 0)}" & $"{NL}Total Size (bytes): {If(result?.BytesStatistic?.Total, 0)}" & $"{NL}Speed (Bytes/Second): {If(result?.SpeedStatistic?.BytesPerSec, 0)}" & $"{NL}Speed (MB/Min): {If(result?.SpeedStatistic?.MegaBytesPerMin, 0)}" & $"{NL}{If(result?.Status.ToString(), "")}"

Dim NL As String = Environment.NewLine
lbl_OverallTotals.Content = $"Job History:" & $"{NL}Total Directories: {JobResults.DirectoriesStatistic.Total}" & $"{NL}Total Files: {JobResults.FilesStatistic.Total}" & $"{NL}Total Size (bytes): {JobResults.BytesStatistic.Total}" & $"{NL}Speed (Bytes/Second): {JobResults.SpeedStatistic.BytesPerSec}" & $"{NL}Speed (MB/Min): {JobResults.SpeedStatistic.MegaBytesPerMin}" & $"{NL}Any Jobs Cancelled: {(If(JobResults.Status.WasCancelled, "YES", "NO"))}" & $"{NL}{JobResults.Status.ToString()}"

.AddStatistic

N.B. The above has been superseded by changes in PR #127 but is still available to be used if required

This is useful if you are running multiple RoboCopy tasks as it allows you to add all the statistics to each other to generating overall results

Dim FileStats As New RoboSharp.Results.Statistic()
Dim DirStats As New RoboSharp.Results.Statistic()

test = New RoboCommand()

' Run first task and add results
test.CopyOptions.Source = "C:\SOURCE_1"
test.CopyOptions.Destination = "C:\DESTINATION"

Dim results1 As RoboSharp.Results.RoboCopyResults = Await test.StartAsync()

FileStats.AddStatistic(results1.FilesStatistic)
DirStats.AddStatistic(results1.DirectoriesStatistic)

' Run second task and add results
test.CopyOptions.Source = "C:\SOURCE_2"
test.CopyOptions.Destination = "C:\DESTINATION"

Dim results2 As RoboSharp.Results.RoboCopyResults = Await test.StartAsync()

FileStats.AddStatistic(results2.FilesStatistic)
DirStats.AddStatistic(results2.DirectoriesStatistic)

You could also use .AddStatistic in the OnCommandCompleted event e.g.

Private Sub copy_OnCommandCompleted(ByVal sender As Object, ByVal e As RoboCommandCompletedEventArgs)
			Me.BeginInvoke(CType(Sub()
				' Get robocopy results
				Dim AnalysisResults As RoboSharp.Results.RoboCopyResults = e.Results

				FileStats.AddStatistic(AnalysisResults.FilesStatistic)
			End Sub, Action))
End Sub

.AverageStatistics

N.B. The above has been replaced by improved methods in PR #127

Clone this wiki locally