Fixed file naming template
This commit is contained in:
parent
e787d33e5a
commit
df583e73c2
@ -1,5 +1,6 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
|
||||||
@ -17,15 +18,36 @@ namespace FileManager
|
|||||||
/// <summary>Generate a valid path for this file or directory</summary>
|
/// <summary>Generate a valid path for this file or directory</summary>
|
||||||
public LongPath GetFilePath(bool returnFirstExisting = false)
|
public LongPath GetFilePath(bool returnFirstExisting = false)
|
||||||
{
|
{
|
||||||
int lastSlash = Template.LastIndexOf('\\');
|
string fileName = Template;
|
||||||
|
List<string> pathParts = new();
|
||||||
var directoryName = lastSlash >= 0 ? Template[..(lastSlash + 1)] : string.Empty;
|
|
||||||
var filename = lastSlash >= 0 ? Template[(lastSlash + 1)..] : Template;
|
|
||||||
|
|
||||||
List<StringBuilder> filenameParts = new();
|
|
||||||
|
|
||||||
var paramReplacements = ParameterReplacements.ToDictionary(r => $"<{formatKey(r.Key)}>", r => formatValue(r.Value));
|
var paramReplacements = ParameterReplacements.ToDictionary(r => $"<{formatKey(r.Key)}>", r => formatValue(r.Value));
|
||||||
|
|
||||||
|
while (!string.IsNullOrEmpty(fileName))
|
||||||
|
{
|
||||||
|
var file = Path.GetFileName(fileName);
|
||||||
|
|
||||||
|
if (Path.IsPathRooted(Template) && file == string.Empty)
|
||||||
|
{
|
||||||
|
pathParts.Add(fileName);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
file = replaceFileName(file, paramReplacements);
|
||||||
|
fileName = Path.GetDirectoryName(fileName);
|
||||||
|
pathParts.Add(file);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pathParts.Reverse();
|
||||||
|
|
||||||
|
return FileUtility.GetValidFilename(string.Join(Path.DirectorySeparatorChar, pathParts), IllegalCharacterReplacements, returnFirstExisting);
|
||||||
|
}
|
||||||
|
|
||||||
|
private string replaceFileName(string filename, Dictionary<string,string> paramReplacements)
|
||||||
|
{
|
||||||
|
List<StringBuilder> filenameParts = new();
|
||||||
//Build the filename in parts, replacing replacement parameters with
|
//Build the filename in parts, replacing replacement parameters with
|
||||||
//their values, and storing the parts in a list.
|
//their values, and storing the parts in a list.
|
||||||
while (!string.IsNullOrEmpty(filename))
|
while (!string.IsNullOrEmpty(filename))
|
||||||
@ -66,10 +88,7 @@ namespace FileManager
|
|||||||
|
|
||||||
maxEntry.Remove(maxLength - 1, 1);
|
maxEntry.Remove(maxLength - 1, 1);
|
||||||
}
|
}
|
||||||
|
return string.Join("", filenameParts);
|
||||||
filename = string.Join("", filenameParts);
|
|
||||||
|
|
||||||
return FileUtility.GetValidFilename(directoryName + filename, IllegalCharacterReplacements, returnFirstExisting);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private string formatValue(object value)
|
private string formatValue(object value)
|
||||||
|
|||||||
@ -140,12 +140,11 @@ namespace LibationWinForms.Dialogs
|
|||||||
richTextBox1.SelectionFont = reg;
|
richTextBox1.SelectionFont = reg;
|
||||||
|
|
||||||
if (isChapterTitle)
|
if (isChapterTitle)
|
||||||
|
{
|
||||||
richTextBox1.SelectionFont = bold;
|
richTextBox1.SelectionFont = bold;
|
||||||
|
|
||||||
richTextBox1.AppendText(chapterTitle);
|
richTextBox1.AppendText(chapterTitle);
|
||||||
|
|
||||||
if (isChapterTitle)
|
|
||||||
return;
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
richTextBox1.AppendText(slashWrap(books));
|
richTextBox1.AppendText(slashWrap(books));
|
||||||
richTextBox1.AppendText(sing);
|
richTextBox1.AppendText(sing);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user