Hi @Cynolycus ,
Use CopySuccess
as a semaphore to record the success of the copy operation.
Create two lists, FilesToCopy
and FilesCopied
, to store the files that need to be copied and the directories that have been copied successfully.
Use a Do... Loop
construct, which runs until the CopySuccess
variable is True. Within the loop, try to copy the file. If an error occurs during the replication process, the copied files are deleted and the entire replication process is restarted until all files have been successfully copied.
Dim sourceDirectory As String = "sourceDirectory"
Dim destinationDirectory As String = "destinationDirectory"
Dim filesToCopy As String() = {"file1.txt" , "file2.txt" , "file3.txt", ...}
Dim copySuccess As Boolean = False
Do
Dim filesCopied As New List(Of String)()
Try
For Each file As String In filesToCopy
Dim sourceFile As String = Path.Combine(sourceDirectory, file)
Dim destinationFile As String = Path.Combine(destinationDirectory, file)
File.Copy(sourceFile, destinationFile, True)
filesCopied.Add(destinationFile)
Next
Console.WriteLine("All files have been copied successfully.")
copySuccess = True
Catch ex As Exception
Console.WriteLine($"Error copying: {ex.Message}")
For Each copiedFile As String In filesCopied
Try
If File.Exists(copiedFile) Then
File.Delete(copiedFile)
Console.WriteLine($"File deleted: {copiedFile}")
End If
Catch deleteEx As Exception
Console.WriteLine($"Error deleting: {deleteEx.Message}")
End Try
Next
End Try
Loop While Not copySuccess
Best Regards.
Jiachen Li
If the answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.