50 lines
1.8 KiB
C#
50 lines
1.8 KiB
C#
using Serilog;
|
|
|
|
namespace File_Folder_Helper.Helpers;
|
|
|
|
internal static class HelperRenameToOldMoveDeleteOldMerge
|
|
{
|
|
|
|
internal static void RenameToOldMoveDeleteOld(ILogger log, string argsZero)
|
|
{
|
|
string checkDirectory = argsZero[0..^1];
|
|
if (!Directory.Exists(checkDirectory))
|
|
log.Information(string.Concat("<", checkDirectory, "> doesn't exist!"));
|
|
else
|
|
{
|
|
string renameFile;
|
|
string destinationFile;
|
|
List<string> deleteFiles = new();
|
|
string[] moveFiles = Directory.GetFiles(argsZero, "*", SearchOption.TopDirectoryOnly);
|
|
log.Information(string.Concat("<", moveFiles.Length, "> to move"));
|
|
foreach (string moveFile in moveFiles)
|
|
{
|
|
destinationFile = string.Concat(checkDirectory, moveFile[argsZero.Length..]);
|
|
if (!File.Exists(destinationFile))
|
|
File.Move(moveFile, destinationFile);
|
|
else
|
|
{
|
|
renameFile = Path.ChangeExtension(destinationFile, string.Concat(Path.GetExtension(destinationFile), ".old"));
|
|
File.Move(destinationFile, renameFile);
|
|
File.Move(moveFile, destinationFile);
|
|
deleteFiles.Add(renameFile);
|
|
}
|
|
}
|
|
log.Information(string.Concat("<", deleteFiles.Count, "> to delete"));
|
|
foreach (string deleteFile in deleteFiles)
|
|
{
|
|
for (short i = 0; i < short.MaxValue; i++)
|
|
{
|
|
try
|
|
{
|
|
File.Delete(deleteFile);
|
|
break;
|
|
}
|
|
catch (Exception) { }
|
|
Thread.Sleep(500);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
} |