using FileFlows.Plugin; using FileFlows.Plugin.Attributes; namespace FileFlows.BasicNodes.File; /// /// A flow element that deletes a single item (file or directory) /// public class Delete : Node { /// /// Gets the number of inputs /// public override int Inputs => 1; /// /// Gets the number of outputs /// public override int Outputs => 1; /// /// Gets the type of flow element /// public override FlowElementType Type => FlowElementType.Process; /// /// Gets the icon for the flow element /// public override string Icon => "far fa-trash-alt"; /// /// Gets the help URL for the flow element /// public override string HelpUrl => "https://fileflows.com/docs/plugins/basic-nodes/delete"; /// /// Gets or sets the FileName/path to delete /// [TextVariable(1)] public string FileName { get; set; } /// /// Executes the flow element /// /// the node parameters /// the next output to call public override int Execute(NodeParameters args) { string path = args.ReplaceVariables(this.FileName ?? string.Empty, stripMissing: true); if (string.IsNullOrEmpty(path)) path = args.WorkingFile; if (args.FileService.DirectoryExists(path).Is(true)) { try { args.Logger?.ILog("Deleting directory: " + path); var result = args.FileService.DirectoryDelete(path, true); if (result.IsFailed) args.Logger?.ELog("Failed deleting directory: "+ result.Error); else args.Logger?.ILog("Deleted directory: " + path); return 1; } catch (Exception ex) { args.Logger?.ELog("Failed to delete directory: " + ex.Message); return -1; } } else { try { args.Logger?.ILog("Deleting file: " + path); var result = args.FileService.FileDelete(path); if(result.IsFailed) args.Logger?.ELog("Failed to Deleted file: " + result.Error); else args.Logger?.ILog("Deleted file: " + path); return 1; } catch (Exception ex) { args.Logger?.ELog($"Failed to delete file: '{path}' => {ex.Message}"); return -1; } } } }