private static List <DeleteResult> ExecuteDelete(DeleteInput input, CancellationToken cancellationToken) { var results = FindMatchingFiles(input.Directory, input.Pattern); var fileResults = new List <DeleteResult>(); try { foreach (var path in results.Files.Select(match => Path.Combine(input.Directory, match.Path))) { cancellationToken.ThrowIfCancellationRequested(); var fileSizeInBytes = new FileInfo(path).Length; fileResults.Add(new DeleteResult(path, fileSizeInBytes)); System.IO.File.Delete(path); } } catch (Exception ex) { var deletedfilesMsg = fileResults.Any() ? ": " + string.Join(",", fileResults.Select(f => f.Path)) : string.Empty; throw new Exception($"Could not delete all files. Error: {ex.Message}. Deleted {fileResults.Count} files{deletedfilesMsg}", ex); } return(fileResults); }
/// <summary> /// Delete files. See: https://github.com/FrendsPlatform/Frends.File#Delete /// </summary> /// <returns>List [ Object { string Path, string SizeInMegaBytes } ]</returns> public static List <DeleteResult> Delete( [PropertyTab] DeleteInput input, [PropertyTab] DeleteOption options, CancellationToken cancellationToken) { return(ExecuteAction(() => ExecuteDelete(input, cancellationToken), options.UseGivenUserCredentialsForRemoteConnections, options.UserName, options.Password)); }