/// <summary> /// Execute a set of copy tasks. /// </summary> /// <param name="copyTasks"></param> /// <returns>The status of the copy tasks. A set of counts of success, fail, etc.</returns> public async Task<SyncCopyResult> ExecuteFileCopyTasks(IEnumerable<CopyTask> copyTasks) { var syncResult = new SyncCopyResult(); syncResult.AlreadyExistedCount = copyTasks.Count(x => x.FileExistsAlready); var itemsToCopy = copyTasks.Where(x => x.FileExistsAlready == false); if (Config.ShouldLogDebug) { await FileHelper.WriteToErrLogAsync("{0} items already exist at destination.".FormatWith(syncResult.AlreadyExistedCount)); await FileHelper.WriteToErrLogAsync("Copying {0} items to {1}".FormatWith(itemsToCopy.Count(), Config.DestinationDir)); } foreach (var copyTask in itemsToCopy) { try { await ExecuteCopyTask(copyTask); syncResult.CopiedSuccessfullyCount += 1; if (Config.ShouldLogDebug) { await FileHelper.WriteToErrLogAsync("File {0} of {1} copied successfully to {2}".FormatWith( syncResult.CopiedSuccessfullyCount, itemsToCopy.Count(), copyTask.DestinationFile)); } } catch (Exception e) { copyTask.CopyStatus = e.Message; syncResult.UncopiedProblemCount += 1; } } return syncResult; }
private void ShowCompletionBalloon(SyncCopyResult result, string targetDir) { string completionMessage = CreateCompletedMessage(result, targetDir); ShowBalloon(completionMessage); }
private string CreateCompletedMessage(SyncCopyResult result, string targetDir) { var msg = new StringBuilder(); msg.AppendLine("Done syncing to {0}.".FormatWith(targetDir)); msg.AppendLine("{0} copied.".FormatWith(result.CopiedSuccessfullyCount)); msg.AppendLine(result.TimeElapsedMsg); if (result.AlreadyExistedCount > 0) { msg.AppendLine("{0} already existed.".FormatWith(result.AlreadyExistedCount)); } if (result.CopiedButAlreadyExistedDiffSizeCount > 0) { msg.AppendLine("{0} new/changed files.".FormatWith(result.CopiedButAlreadyExistedDiffSizeCount)); } if (result.UncopiedProblemCount > 0) { msg.AppendLine("{0} had problems copying.".FormatWith(result.UncopiedProblemCount)); } return msg.ToString(); }
private async Task Sync(SyncConfig syncConfig) { bool shouldLog = syncConfig.ShouldLogDebug; //do a sanity check on the config to avoid exceptions while processing if (ValidateConfigCreateMissingConfigMessage(syncConfig).HasValidConfig == false) { return; } Stopwatch stopwatch = new Stopwatch(); try { ShowBalloon("Finding items..."); LastRunOn = DateTime.Now; lastRunToolStripMenuItem.Text = "Syncing."; stopwatch.Start(); var fileSync = new FileSyncer(syncConfig); var copyResult = new SyncCopyResult(); List<CopyTask> copyTasks = fileSync.BuildFileCopyTasks().ToList(); if (shouldLog) { await FileHelper.WriteToErrLogAsync("Found {0} items to sync.".FormatWith(copyTasks.Count)); } if (copyTasks.Any()) { ShowBalloon("Copying {0} items...".FormatWith(copyTasks.Count)); copyResult = await fileSync.ExecuteFileCopyTasks(copyTasks); if (shouldLog) { await FileHelper.WriteToErrLogAsync("Completed file copy tasks"); } await DeleteSourceFilesIfRequired(syncConfig, copyTasks); } stopwatch.Stop(); copyResult.TimeElapsedMsg = "Total sync time was {0}:{1}".FormatWith( stopwatch.Elapsed.Minutes.ToString("00"), stopwatch.Elapsed.Seconds.ToString("00")); if (shouldLog) { await FileHelper.WriteToErrLogAsync(copyResult.TimeElapsedMsg); } ShowCompletionBalloon(copyResult, syncConfig.DestinationDir); } catch (Exception ex) { var fileIOHelper = new FileIOHelper(); fileIOHelper.WriteToErrLog(ex.Message); ShowBalloon("Sorry we had a problem when syncing. The log file has more details. " + FileIOHelper.OutputLogFile, 10); } }