40 lines
1.6 KiB
C#
40 lines
1.6 KiB
C#
using Microsoft.Extensions.Logging;
|
|
namespace File_Folder_Helper.ADO2024.PI3;
|
|
|
|
internal static partial class Helper20240820
|
|
{
|
|
|
|
internal static void MoveFilesWithSleep(ILogger<Worker> logger, List<string> args)
|
|
{
|
|
string checkFile;
|
|
string checkDirectory;
|
|
int sleep = int.Parse(args[4]);
|
|
string searchPattern = args[3];
|
|
string sourceDirectory = args[0];
|
|
string destinationDirectory = args[2];
|
|
string source = Path.GetFullPath(sourceDirectory);
|
|
FileInfo[] collection = Directory.GetFiles(source, "*", SearchOption.TopDirectoryOnly).Select(l => new FileInfo(l)).ToArray();
|
|
string[] files = (from l in collection orderby l.LastWriteTime select l.FullName).ToArray();
|
|
logger.LogInformation("With search pattern '{SearchPattern}' found {files}", searchPattern, files.Length);
|
|
foreach (string file in files)
|
|
{
|
|
Thread.Sleep(500);
|
|
checkFile = file.Replace(source, destinationDirectory);
|
|
if (checkFile == file)
|
|
throw new NotSupportedException("Replace failed!");
|
|
checkDirectory = Path.GetDirectoryName(checkFile) ?? throw new NotSupportedException();
|
|
try
|
|
{
|
|
if (!Directory.Exists(checkDirectory))
|
|
_ = Directory.CreateDirectory(checkDirectory);
|
|
if (File.Exists(checkFile))
|
|
continue;
|
|
File.Move(file, checkFile);
|
|
Thread.Sleep(sleep);
|
|
}
|
|
catch (Exception ex)
|
|
{ logger.LogInformation(ex, "Inner loop error!"); }
|
|
}
|
|
}
|
|
|
|
} |