BangumiRenamer/Src/Tools/FolderCloner.cs
2025-11-08 20:04:11 +08:00

80 lines
2.2 KiB
C#

using System.Diagnostics;
using BangumiRenamer.Utils;
using NativeFileDialogSharp;
namespace BangumiRenamer.Tools;
public static class FolderCloner
{
private static readonly Lazy<Log> _lazy = new (() =>
{
var log = new Log(new Log.LogConfig());
if (!log.Init()) return null;
return log;
});
static ILog Log => _lazy.Value;
public static void Run()
{
var result = Dialog.FolderPicker();
if (!result.IsOk) return;
var source = result.Path;
result = Dialog.FolderPicker();
if (!result.IsOk) return;
var dest = Path.Combine(result.Path, Path.GetFileNameWithoutExtension(source.Replace("\\\\", "")));
var finalDest = dest;
int suffix = 1;
while (Directory.Exists(finalDest))
{
finalDest = dest + suffix;
++suffix;
}
CloneStructureWithEmptyFiles(source, finalDest);
Process.Start(new ProcessStartInfo
{
FileName = finalDest,
UseShellExecute = true,
Verb = "open"
});
}
static void CloneStructureWithEmptyFiles(string source, string dest)
{
if (!Directory.Exists(source))
{
return;
}
if (Directory.Exists(dest))
{
return;
}
var queue = new Queue<string>();
queue.Enqueue(source);
while (queue.Count > 0)
{
var dir = queue.Dequeue();
try
{
foreach (var filePath in Directory.GetFiles(dir))
{
var newFilePath = Path.Combine(dest, Path.GetRelativePath(source, filePath));
using var _ = File.Create(newFilePath);
}
foreach (var dirPath in Directory.GetDirectories(dir))
{
queue.Enqueue(dirPath);
var newDirPath = Path.Combine(dest, Path.GetRelativePath(source, dirPath));
Directory.CreateDirectory(newDirPath);
}
}
catch (Exception ex)
{
Log.Error(ex, "Clone");
}
}
}
}