Skip to content

armgit / Filee

Public Imported from GitHub
0

Free, open-source file converter. Convert 263 formats offline by drag & drop — images, PDF, Word, Excel, PowerPoint, HWP/HWPX, video, audio, e-books and archives. No upload, no Office needed.

Watch folders keep subfolders in the output folder

SeoJCalived97a67cf4c33
diff --git a/src/Filee.Cli/Cli.cs b/src/Filee.Cli/Cli.cs
index 45727f35..3580a7bb 100644
--- a/src/Filee.Cli/Cli.cs
+++ b/src/Filee.Cli/Cli.cs
@@ -93,6 +93,7 @@ internal static class Cli
Originals = options.MoveOriginals ? AfterConversion.MoveToOriginals : AfterConversion.Keep,
};
UseFolder(preset, rule.ResolvedOutputFolder);
+ preset.Output.KeepFoldersFrom = rule.Folder; // as the app's watch folders: subfolders stay subfolders
var settle = options.SettleSeconds is { } seconds ? TimeSpan.FromSeconds(seconds) : (TimeSpan?)null;
await using var watcher = new FolderWatcher(rule,
diff --git a/src/Filee.Cli/Help.cs b/src/Filee.Cli/Help.cs
index 9893c579..3c4411e5 100644
--- a/src/Filee.Cli/Help.cs
+++ b/src/Filee.Cli/Help.cs
@@ -70,7 +70,7 @@ internal static class Help
-t, --to <format> Target format
-p, --preset <preset> A preset from the app, by id or name
-o, --output <folder> Where converted files go (default: <folder>\converted)
- -r, --recursive Also watch sub folders
+ -r, --recursive Also watch sub folders (their files keep the sub folder in the output)
--move-originals Move converted sources to <folder>\originals; files already waiting are
converted at start
--settle <seconds> How long a file must stay unchanged (default 2)
diff --git a/src/Filee.Core/Presets/OutputPathResolver.cs b/src/Filee.Core/Presets/OutputPathResolver.cs
index be779bff..bdf3c108 100644
--- a/src/Filee.Core/Presets/OutputPathResolver.cs
+++ b/src/Filee.Core/Presets/OutputPathResolver.cs
@@ -28,7 +28,8 @@ public static class OutputPathResolver
var dir = rule.Location switch
{
OutputLocation.Subfolder => Path.Combine(sourceDir, Sanitize(ExpandPattern(rule.SubfolderName, tokens), "converted")),
- OutputLocation.CustomFolder when !string.IsNullOrWhiteSpace(rule.CustomFolder) => rule.CustomFolder,
+ OutputLocation.CustomFolder when !string.IsNullOrWhiteSpace(rule.CustomFolder) =>
+ Path.Combine(rule.CustomFolder, SubfolderBelow(rule.KeepFoldersFrom, sourceDir)),
_ => sourceDir,
};
@@ -56,6 +57,20 @@ public static class OutputPathResolver
}
}
+ /// <summary>
+ /// The path of <paramref name="folder"/> below <paramref name="root"/> (<c>2025\scans</c>), or empty when there is no
+ /// root, or the folder is the root itself or lies outside it.
+ /// </summary>
+ private static string SubfolderBelow(string root, string folder)
+ {
+ if (string.IsNullOrWhiteSpace(root))
+ return "";
+ var relative = Path.GetRelativePath(Path.GetFullPath(root), folder);
+ return relative == "." || Path.IsPathRooted(relative) || relative == ".." || relative.StartsWith(".." + Path.DirectorySeparatorChar, StringComparison.Ordinal)
+ ? ""
+ : relative;
+ }
+
/// <summary>Applies <paramref name="steps"/> to <paramref name="name"/> in order.</summary>
public static string Rename(string name, IEnumerable<RenameStep> steps) =>
steps.Aggregate(name, (current, step) => step.Apply(current));
diff --git a/src/Filee.Core/Presets/OutputRule.cs b/src/Filee.Core/Presets/OutputRule.cs
index 735a15fb..38a7897f 100644
--- a/src/Filee.Core/Presets/OutputRule.cs
+++ b/src/Filee.Core/Presets/OutputRule.cs
@@ -38,6 +38,14 @@ public sealed class OutputRule
public string CustomFolder { get; set; } = "";
+ /// <summary>
+ /// With <see cref="OutputLocation.CustomFolder"/>: a source inside this folder keeps its subfolder below
+ /// <see cref="CustomFolder"/> (<c>Inbox\2025\a.jpg</c> → <c>converted\2025\a.pdf</c>), so files of the same name in
+ /// different subfolders don't end up side by side as <c>a.pdf</c> and <c>a (2).pdf</c>. Empty: everything goes
+ /// straight into <see cref="CustomFolder"/>. Watch folders set it to the watched folder.
+ /// </summary>
+ public string KeepFoldersFrom { get; set; } = "";
+
/// <summary>
/// File name pattern without extension. Tokens:
/// <c>{name}</c> source name, <c>{ext}</c> source extension, <c>{preset}</c> preset name,
diff --git a/src/Filee.Core/Watching/WatchRule.cs b/src/Filee.Core/Watching/WatchRule.cs
index d134e876..8aa65134 100644
--- a/src/Filee.Core/Watching/WatchRule.cs
+++ b/src/Filee.Core/Watching/WatchRule.cs
@@ -59,14 +59,16 @@ public sealed class WatchRule
public string OriginalsFolder => Path.Combine(Folder, OriginalsName);
/// <summary>
- /// The preset as this folder converts with it: output into <see cref="ResolvedOutputFolder"/>, and the folder's
- /// own name rules where set.
+ /// The preset as this folder converts with it: output into <see cref="ResolvedOutputFolder"/>, files from
+ /// subfolders into the same subfolders there (as <see cref="AfterConversion.MoveToOriginals"/> does with the
+ /// sources), and the folder's own name rules where set.
/// </summary>
public Preset Apply(Preset preset)
{
var applied = preset.Clone();
applied.Output.Location = OutputLocation.CustomFolder;
applied.Output.CustomFolder = ResolvedOutputFolder;
+ applied.Output.KeepFoldersFrom = Folder;
if (!string.IsNullOrWhiteSpace(FileNamePattern))
applied.Output.FileNamePattern = FileNamePattern.Trim();
// Steps without a pattern are being typed; leaving them out keeps the name as it is.
diff --git a/tests/Filee.Core.Tests/WatchRuleOptionsTests.cs b/tests/Filee.Core.Tests/WatchRuleOptionsTests.cs
index 2c5110c3..7a0f5f6b 100644
--- a/tests/Filee.Core.Tests/WatchRuleOptionsTests.cs
+++ b/tests/Filee.Core.Tests/WatchRuleOptionsTests.cs
@@ -85,6 +85,41 @@ public class WatchRuleOptionsTests
Assert.Empty(preset.Output.Renames);
}
+ [Fact]
+ public void Files_from_subfolders_keep_their_subfolder_in_the_output_folder()
+ {
+ // Issue #72: Inbox\2024\a.jpg and Inbox\2025\a.jpg used to become converted\a.pdf and converted\a (2).pdf.
+ using var dir = new TempDir();
+ var preset = new Preset { TargetFormat = "pdf" };
+ var output = Path.Combine(dir.Path, "Out");
+ var applied = new WatchRule { Folder = dir.Path, IncludeSubfolders = true, OutputFolder = output }.Apply(preset);
+ string? Resolve(string source) => OutputPathResolver.Resolve(applied.Output,
+ new OutputPathResolver.Tokens(source, "PDF", 1, DateTime.Now), "pdf", File.Exists);
+
+ Assert.Equal(Path.Combine(output, "2024", "a.pdf"), Resolve(Path.Combine(dir.Path, "2024", "a.jpg")));
+ Assert.Equal(Path.Combine(output, "2025", "scans", "a.pdf"), Resolve(Path.Combine(dir.Path, "2025", "scans", "a.jpg")));
+ Assert.Equal(Path.Combine(output, "top.pdf"), Resolve(Path.Combine(dir.Path, "top.jpg"))); // top level: no subfolder
+
+ // The default output folder inside the watched folder works the same way.
+ var inside = new WatchRule { Folder = dir.Path, IncludeSubfolders = true }.Apply(preset);
+ Assert.Equal(Path.Combine(dir.Path, "converted", "2025", "a.pdf"), OutputPathResolver.Resolve(inside.Output,
+ new OutputPathResolver.Tokens(Path.Combine(dir.Path, "2025", "a.jpg"), "PDF", 1, DateTime.Now), "pdf", File.Exists));
+ }
+
+ [Fact]
+ public void A_custom_folder_without_a_root_or_a_source_outside_it_gets_no_subfolder()
+ {
+ var output = Path.Combine(Path.GetTempPath(), "Out");
+ var inbox = Path.Combine(Path.GetTempPath(), "Inbox");
+ var elsewhere = Path.Combine(Path.GetTempPath(), "Elsewhere", "2025", "a.jpg");
+ string? Resolve(string keepFrom, string source) => OutputPathResolver.Resolve(
+ new OutputRule { Location = OutputLocation.CustomFolder, CustomFolder = output, KeepFoldersFrom = keepFrom },
+ new OutputPathResolver.Tokens(source, "PDF", 1, DateTime.Now), "pdf", _ => false);
+
+ Assert.Equal(Path.Combine(output, "a.pdf"), Resolve("", Path.Combine(inbox, "2025", "a.jpg"))); // presets: as before
+ Assert.Equal(Path.Combine(output, "a.pdf"), Resolve(inbox, elsewhere)); // a retry of a file moved away
+ }
+
[Fact]
public void Rename_steps_are_applied_in_order()
{