mirror of
https://github.com/revenz/FileFlowsPlugins.git
synced 2026-02-15 20:48:34 -06:00
FF-131 - renamed pattern repalcer to filename pattern replacer
This commit is contained in:
Binary file not shown.
Binary file not shown.
@@ -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",
|
||||
|
||||
77
BasicNodes/File/PatternReplacer.cs
Normal file
77
BasicNodes/File/PatternReplacer.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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.
BIN
Emby/Emby.csproj
BIN
Emby/Emby.csproj
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.
BIN
Plex/Plex.csproj
BIN
Plex/Plex.csproj
Binary file not shown.
Binary file not shown.
Binary file not shown.
Reference in New Issue
Block a user