FF-131 - renamed pattern repalcer to filename pattern replacer

This commit is contained in:
John Andrews
2022-06-23 19:33:52 +12:00
parent 9e9e37827f
commit 148076697b
19 changed files with 78 additions and 76 deletions

Binary file not shown.

Binary file not shown.

View File

@@ -209,6 +209,7 @@
}
},
"PatternReplacer": {
"Title": "Filename Pattern Replacer",
"Description": "Lets you make replacements in the filename. Can use regular expressions for replacements, or simple string replacements.\n\nOutput 1: Replacement done\nOutput 2: No replacement done",
"Outputs": {
"1": "Replacement done",

View File

@@ -0,0 +1,77 @@
namespace FileFlows.BasicNodes.Functions;
using System.ComponentModel.DataAnnotations;
using System.Text.RegularExpressions;
using FileFlows.Plugin;
using FileFlows.Plugin.Attributes;
public class PatternReplacer : Node
{
public override int Inputs => 1;
public override int Outputs => 2;
public override FlowElementType Type => FlowElementType.Process;
public override string Icon => "fas fa-exchange-alt";
public override string HelpUrl => "https://docs.fileflows.com/plugins/basic-nodes/filename-pattern-replacer";
internal bool UnitTest = false;
[KeyValue(1)]
[Required]
public List<KeyValuePair<string, string>> Replacements{ get; set; }
[Boolean(2)]
public bool UseWorkingFileName { get; set; }
public override int Execute(NodeParameters args)
{
if (Replacements?.Any() != true)
return 2; // no replacements
string filename = new FileInfo(UseWorkingFileName ? args.WorkingFile : args.FileName).Name;
string updated = filename;
try
{
foreach(var replacement in Replacements)
{
try
{
// this might not be a regex, but try it first
updated = Regex.Replace(updated, replacement.Key, replacement.Value, RegexOptions.IgnoreCase);
}
catch (Exception ex) { }
updated = updated.Replace(replacement.Key, replacement.Value);
}
if (updated == filename)
{
args.Logger?.ILog("No replacements found in file: " + filename);
return 2; // did not match
}
args.Logger?.ILog($"Pattern replacement: '{filename}' to '{updated}'");
string dest = Path.Combine(new FileInfo(args.WorkingFile)?.DirectoryName ?? string.Empty, updated);
args.Logger?.ILog($"New filename: " + dest);
if (UnitTest == false)
{
if (args.MoveFile(dest) == false)
{
args.Logger?.ELog("Failed to move file");
return -1;
}
}
else
{
args.SetWorkingFile(dest);
}
return 1;
}
catch (Exception ex)
{
args.Logger?.ELog("Pattern error: " + ex.Message);
return -1;
}
}
}

View File

@@ -1,76 +0,0 @@
namespace FileFlows.BasicNodes.Functions
{
using System.ComponentModel.DataAnnotations;
using System.Text.RegularExpressions;
using FileFlows.Plugin;
using FileFlows.Plugin.Attributes;
public class PatternReplacer : Node
{
public override int Inputs => 1;
public override int Outputs => 2;
public override FlowElementType Type => FlowElementType.Logic;
public override string Icon => "fas fa-exchange-alt";
internal bool UnitTest = false;
[KeyValue(1)]
[Required]
public List<KeyValuePair<string, string>> Replacements{ get; set; }
[Boolean(2)]
public bool UseWorkingFileName { get; set; }
public override int Execute(NodeParameters args)
{
if (Replacements?.Any() != true)
return 2; // no replacements
string filename = new FileInfo(UseWorkingFileName ? args.WorkingFile : args.FileName).Name;
string updated = filename;
try
{
foreach(var replacement in Replacements)
{
try
{
// this might not be a regex, but try it first
updated = Regex.Replace(updated, replacement.Key, replacement.Value, RegexOptions.IgnoreCase);
}
catch (Exception ex) { }
updated = updated.Replace(replacement.Key, replacement.Value);
}
if (updated == filename)
{
args.Logger?.ILog("No replacements found in file: " + filename);
return 2; // did not match
}
args.Logger?.ILog($"Pattern replacement: '{filename}' to '{updated}'");
string dest = Path.Combine(new FileInfo(args.WorkingFile)?.DirectoryName ?? string.Empty, updated);
args.Logger?.ILog($"New filename: " + dest);
if (UnitTest == false)
{
if (args.MoveFile(dest) == false)
{
args.Logger?.ELog("Failed to move file");
return -1;
}
}
else
{
args.SetWorkingFile(dest);
}
return 1;
}
catch (Exception ex)
{
args.Logger?.ELog("Pattern error: " + ex.Message);
return -1;
}
}
}
}

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.