public async Task OrganizeAsync(CancellationToken token) { if (token.IsCancellationRequested) { return; } var moverOptions = new FileMoverOptions { RemoveSourceAfterMove = this.Options.RemoveSource }; CreateDestinationIfNotExist(this.Options.DestinationRoot); IList <Task> moveTasks = new List <Task>(); foreach (string file in this.FileEnumerator.GetFilesAsync(this.Options.SourceRoot)) { if (token.IsCancellationRequested) { return; } var destinationPath = this.Mapper.GetDestination(file); if (destinationPath == null) { // This file should not be moved continue; } moveTasks.Add(this.FileMover.MoveAsync(moverOptions, file, destinationPath)); } await Task.WhenAll(moveTasks); }
public Task MoveAsync(FileMoverOptions options, string from, string to) { if (options == null) { throw new ArgumentNullException(nameof(options)); } if (string.Equals(from, to, StringComparison.InvariantCultureIgnoreCase)) { return(Task.CompletedTask); } if (options.SkipIfFileExists && File.Exists(to)) { return(Task.CompletedTask); } return(Task.Run(() => { try { if (options.RemoveSourceAfterMove) { this.logger.LogInformation($"Moving {from} to {to}"); File.Move(from, to); } else { this.logger.LogInformation($"Copying {from} to {to}"); File.Copy(from, to); } } catch (Exception ex) { this.logger.LogWarning($"Failed to process file {from}. Reason: {ex.Message}"); } })); }