Rename files to padded number string

This commit is contained in:
2023-07-09 23:36:39 -07:00
parent d7ed5d89d9
commit 818a1b0b38
60 changed files with 1118 additions and 1455 deletions

View File

@ -0,0 +1,8 @@
namespace View_by_Distance.Shared.Models.Methods;
public interface IMetadata<T>
{
(DateTime?, DateTime?[]) GetDateTimes(FileHolder fileHolder, IReadOnlyList<T> directories);
}

View File

@ -6,6 +6,11 @@ public interface IDirectory
static int GetOffset() =>
1000000;
int TestStatic_GetSortOrderOnlyLengthIndex() =>
GetSortOrderOnlyLengthIndex();
static int GetSortOrderOnlyLengthIndex() =>
GetOffset().ToString().Length + 3;
char TestStatic_GetDirectory(string fileName) =>
GetDirectory(fileName);
static char GetDirectory(string fileName) =>
@ -16,10 +21,23 @@ public interface IDirectory
static int GetDirectory(char directory) =>
directory == '-' ? 10 : int.TryParse(directory.ToString(), out int value) ? value : 11;
string TestStatic_GetPaddedId(int length, int index, int id) =>
GetPaddedId(length, index, id);
static string GetPaddedId(int length, int index, int id) =>
id > -1 ? $"{index}070{id.ToString().PadLeft(length, '0')}" : $"{index}030{id.ToString()[1..].PadLeft(length, '0')}";
string TestStatic_GetPaddedId(int intMinValueLength, int index, int id) =>
GetPaddedId(intMinValueLength, index, id);
static string GetPaddedId(int intMinValueLength, int index, int id) =>
id > -1 ? $"{index}070{id.ToString().PadLeft(intMinValueLength, '0')}" : $"{index}030{id.ToString()[1..].PadLeft(intMinValueLength, '0')}";
bool TestStatic_NameWithoutExtensionIsPaddedIdFormat(string fileNameWithoutExtension, int sortOrderOnlyLengthIndex) =>
NameWithoutExtensionIsPaddedIdFormat(fileNameWithoutExtension, sortOrderOnlyLengthIndex);
static bool NameWithoutExtensionIsPaddedIdFormat(string fileNameWithoutExtension, int sortOrderOnlyLengthIndex) =>
fileNameWithoutExtension.Length > sortOrderOnlyLengthIndex
&& fileNameWithoutExtension[sortOrderOnlyLengthIndex] == '0'
&& fileNameWithoutExtension[sortOrderOnlyLengthIndex - 3] == '0'
&& fileNameWithoutExtension.All(l => char.IsNumber(l));
bool TestStatic_NameWithoutExtensionIsPaddedIdFormat(Models.FileHolder fileHolder, int sortOrderOnlyLengthIndex) =>
NameWithoutExtensionIsPaddedIdFormat(fileHolder, sortOrderOnlyLengthIndex);
static bool NameWithoutExtensionIsPaddedIdFormat(Models.FileHolder fileHolder, int sortOrderOnlyLengthIndex) =>
NameWithoutExtensionIsPaddedIdFormat(fileHolder.NameWithoutExtension, sortOrderOnlyLengthIndex);
List<string[]> TestStatic_GetFilesCollection(string directory, string directorySearchFilter, string fileSearchFilter) =>
GetFilesCollection(directory, directorySearchFilter, fileSearchFilter);
@ -51,6 +69,11 @@ public interface IDirectory
static void MoveFiles(List<string> files, string find, string replace) =>
XDirectory.MoveFiles(files, find, replace);
(string[], List<(Models.FileHolder, string)>) TestStatic_GetToDoCollection(Properties.IPropertyConfiguration propertyConfiguration, List<string[]> filesCollection, string[] directories, Action? tick) =>
GetToDoCollection(propertyConfiguration, filesCollection, directories, tick);
static (string[], List<(Models.FileHolder, string)>) GetToDoCollection(Properties.IPropertyConfiguration propertyConfiguration, List<string[]> filesCollection, string[] directories, Action? tick) =>
XDirectory.GetToDoCollection(propertyConfiguration, copyDuplicates: false, ifCanUseId: true, filesCollection, directories, tick);
(string[], List<(Models.FileHolder, string)>) TestStatic_GetToDoCollection(Properties.IPropertyConfiguration propertyConfiguration, bool copyDuplicates, bool ifCanUseId, List<string[]> filesCollection, string[] directories, Action? tick) =>
GetToDoCollection(propertyConfiguration, copyDuplicates, ifCanUseId, filesCollection, directories, tick);
static (string[], List<(Models.FileHolder, string)>) GetToDoCollection(Properties.IPropertyConfiguration propertyConfiguration, bool copyDuplicates, bool ifCanUseId, List<string[]> filesCollection, string[] directories, Action? tick) =>

View File

@ -1,24 +1,8 @@
using System.Drawing.Imaging;
using System.Reflection;
namespace View_by_Distance.Shared.Models.Stateless.Methods;
public interface IProperty
{ // ...
string TestStatic_DateTimeFormat();
static string DateTimeFormat() => "yyyy:MM:dd HH:mm:ss";
int TestStatic_GetDeterministicHashCode(byte[] value) =>
GetDeterministicHashCode(value);
static int GetDeterministicHashCode(byte[] value) =>
Property.GetDeterministicHashCode(value);
DateTime? TestStatic_GetDateTime(string dateTimeFormat, string? value) =>
GetDateTime(dateTimeFormat, value);
static DateTime? GetDateTime(string dateTimeFormat, string? value) =>
Property.GetDateTime(dateTimeFormat, value);
DateTime TestStatic_GetDateTime(Models.Property? property) =>
GetDateTime(property);
static DateTime GetDateTime(Models.Property? property) =>
@ -54,26 +38,11 @@ public interface IProperty
static (bool?, string[]) IsWrongYear(Models.FileHolder fileHolder, DateTime? dateTimeOriginal, List<DateTime> dateTimes) =>
Property.IsWrongYear(fileHolder, dateTimeOriginal, dateTimes);
(DateTime?, DateTime?[], int?, string?) TestStatic_Get(Models.FileHolder fileHolder, bool isIgnoreExtension, bool isValidImageFormatExtension, bool populateId) =>
Get(fileHolder, isIgnoreExtension, isValidImageFormatExtension, populateId);
static (DateTime?, DateTime?[], int?, string?) Get(Models.FileHolder fileHolder, bool isIgnoreExtension, bool isValidImageFormatExtension, bool populateId) =>
Property.Get(fileHolder, isIgnoreExtension, isValidImageFormatExtension, populateId);
DateTime? TestStatic_GetDateTimeFromName(Models.FileHolder fileHolder) =>
GetDateTimeFromName(fileHolder);
static DateTime? GetDateTimeFromName(Models.FileHolder fileHolder) =>
Property.GetDateTimeFromName(fileHolder);
bool TestStatic_NameWithoutExtensionIsIdFormat(string fileNameWithoutExtension) =>
NameWithoutExtensionIsIdFormat(fileNameWithoutExtension);
static bool NameWithoutExtensionIsIdFormat(string fileNameWithoutExtension) =>
Property.NameWithoutExtensionIsIdFormat(fileNameWithoutExtension);
bool TestStatic_NameWithoutExtensionIsIdFormat(string[] validImageFormatExtensions, Models.FileHolder fileHolder) =>
NameWithoutExtensionIsIdFormat(validImageFormatExtensions, fileHolder);
static bool NameWithoutExtensionIsIdFormat(string[] validImageFormatExtensions, Models.FileHolder fileHolder) =>
Property.NameWithoutExtensionIsIdFormat(validImageFormatExtensions, fileHolder);
bool TestStatic_NameWithoutExtensionIsIdFormat(Models.FileHolder fileHolder) =>
NameWithoutExtensionIsIdFormat(fileHolder);
static bool NameWithoutExtensionIsIdFormat(Models.FileHolder fileHolder) =>
@ -104,14 +73,4 @@ public interface IProperty
static List<DateTime> GetDateTimes(DateTime creationTime, DateTime lastWriteTime, DateTime? dateTime, DateTime? dateTimeDigitized, DateTime? dateTimeFromName, DateTime? dateTimeOriginal, DateTime? gpsDateStamp) =>
Property.GetDateTimes(creationTime, lastWriteTime, dateTime, dateTimeDigitized, dateTimeFromName, dateTimeOriginal, gpsDateStamp);
byte[] TestStatic_GetBytes(string value) =>
GetBytes(value);
static byte[] GetBytes(string value) =>
Property.GetBytes(value);
PropertyItem TestStatic_GetPropertyItem(ConstructorInfo constructorInfo, int id, short type, string value) =>
GetPropertyItem(constructorInfo, id, type, value);
static PropertyItem GetPropertyItem(ConstructorInfo constructorInfo, int id, short type, string value) =>
Property.GetPropertyItem(constructorInfo, id, type, value);
}

View File

@ -1,11 +1,3 @@
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.Globalization;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
namespace View_by_Distance.Shared.Models.Stateless.Methods;
internal abstract class Property
@ -24,25 +16,6 @@ internal abstract class Property
return result;
}
internal static int GetDeterministicHashCode(byte[] value)
{
int result;
unchecked
{
int hash1 = (5381 << 16) + 5381;
int hash2 = hash1;
for (int i = 0; i < value.Length; i += 2)
{
hash1 = ((hash1 << 5) + hash1) ^ value[i];
if (i == value.Length - 1)
break;
hash2 = ((hash2 << 5) + hash2) ^ value[i + 1];
}
result = hash1 + (hash2 * 1566083941);
}
return result;
}
internal static (bool?, string[]) IsWrongYear(string[] segments, string year)
{
bool? result;
@ -113,57 +86,6 @@ internal abstract class Property
return new(result, results);
}
internal static DateTime? GetDateTimeFromName(Models.FileHolder fileHolder)
{
DateTime? result = null;
int length;
string format;
string fullFormat;
StringBuilder value = new();
const string ticksExample = "##################";
string[][] dateFormats = new string[][]
{
new string[] { string.Empty, "yyyyMMdd_HHmmss", string.Empty },
new string[] { string.Empty, "yyyyMMddHHmmssfff", string.Empty },
new string[] { string.Empty, "yyyyMMdd_", ticksExample },
new string[] { string.Empty, "yyyy-MM-dd_", ticksExample },
new string[] { string.Empty, "yyyy-MM-dd.", ticksExample },
new string[] { string.Empty, "yyyy-MM-dd.", $"{ticksExample}.{fileHolder.Length}" },
new string[] { string.Empty, "yyyy-MM-dd HH.mm.ss", string.Empty },
new string[] { string.Empty, "yyyyMMdd_HHmmss", "_LLS" },
new string[] { string.Empty, "yyyyMMdd_HHmmss", "_HDR" },
new string[] { "WIN_", "yyyyMMdd_HH_mm_ss", "_Pro" },
new string[] { "IMG_", "yyyyMMdd_HHmmss", string.Empty },
new string[] { "IMG#####-", "yyyyMMdd-HHmm", string.Empty },
new string[] { "CameraZOOM-", "yyyyMMddHHmmss", string.Empty },
new string[] { "VideoCapture_", "yyyyMMdd-HHmmss ", string.Empty }
};
foreach (string[] dateFormat in dateFormats)
{
_ = value.Clear();
if (dateFormat.Length != 3)
throw new Exception();
fullFormat = string.Join(string.Empty, dateFormat);
if (fileHolder.NameWithoutExtension.Length != fullFormat.Length)
continue;
format = dateFormat[1];
length = dateFormat[0].Length + dateFormat[1].Length;
for (int i = dateFormat[0].Length; i < length; i++)
_ = value.Append(fileHolder.NameWithoutExtension[i]);
if (value.Length != format.Length)
continue;
if (DateTime.TryParseExact(value.ToString(), format, CultureInfo.InvariantCulture, DateTimeStyles.None, out DateTime checkDateTime))
{
if (fileHolder.NameWithoutExtension.Length < ticksExample.Length || !long.TryParse(fileHolder.NameWithoutExtension[^ticksExample.Length..], out long ticks))
result = checkDateTime;
else
result = new DateTime(ticks);
break;
}
}
return result;
}
internal static List<DateTime> GetDateTimes(DateTime creationTime, DateTime lastWriteTime, DateTime? dateTime, DateTime? dateTimeDigitized, DateTime? dateTimeFromName, DateTime? dateTimeOriginal, DateTime? gpsDateStamp)
{
List<DateTime> results = new()
@ -355,7 +277,8 @@ internal abstract class Property
internal static bool NameWithoutExtensionIsIdFormat(string fileNameWithoutExtension)
{
bool result;
if (fileNameWithoutExtension.Length < 5)
int intMinValueLength = int.MinValue.ToString().Length;
if (fileNameWithoutExtension.Length < 5 || fileNameWithoutExtension.Length > intMinValueLength)
result = false;
else
{
@ -365,181 +288,4 @@ internal abstract class Property
return result;
}
internal static bool NameWithoutExtensionIsIdFormat(string[] validImageFormatExtensions, Models.FileHolder fileHolder)
{
bool result;
bool changed;
string? fileNameWithoutExtension = fileHolder.NameWithoutExtension;
for (int i = 0; i < validImageFormatExtensions.Length; i++)
{
changed = false;
foreach (string validImageFormatExtension in validImageFormatExtensions)
{
if (fileNameWithoutExtension.EndsWith(validImageFormatExtension))
{
changed = true;
fileNameWithoutExtension = fileNameWithoutExtension[..^validImageFormatExtension.Length];
break;
}
}
if (!changed)
break;
}
result = NameWithoutExtensionIsIdFormat(fileNameWithoutExtension);
return result;
}
#pragma warning disable CA1416
internal static (DateTime?, DateTime?[], int?, string?) Get(Models.FileHolder fileHolder, bool isIgnoreExtension, bool isValidImageFormatExtension, bool populateId)
{
int? id = null;
string? message = null;
DateTime? dateTime = null;
DateTime? gpsDateStamp = null;
DateTime? dateTimeOriginal = null;
DateTime? dateTimeDigitized = null;
if (!isIgnoreExtension && isValidImageFormatExtension)
{
try
{
byte[] bytes;
string value;
DateTime checkDateTime;
PropertyItem? propertyItem;
ASCIIEncoding asciiEncoding = new();
string dateTimeFormat = IProperty.DateTimeFormat();
using Image image = Image.FromFile(fileHolder.FullName);
if (!populateId)
id = null;
else
{
using Bitmap bitmap = new(image);
Rectangle rectangle = new(0, 0, image.Width, image.Height);
BitmapData bitmapData = bitmap.LockBits(rectangle, ImageLockMode.ReadOnly, bitmap.PixelFormat);
IntPtr intPtr = bitmapData.Scan0;
int length = bitmapData.Stride * bitmap.Height;
bytes = new byte[length];
Marshal.Copy(intPtr, bytes, 0, length);
bitmap.UnlockBits(bitmapData);
id = IProperty.GetDeterministicHashCode(bytes);
bitmap.Dispose();
}
if (image.PropertyIdList.Contains((int)IExif.Tags.DateTime))
{
propertyItem = image.GetPropertyItem((int)IExif.Tags.DateTime);
if (propertyItem?.Value is not null)
{
value = asciiEncoding.GetString(propertyItem.Value, 0, propertyItem.Len - 1);
if (value.Length > dateTimeFormat.Length)
value = value[..dateTimeFormat.Length];
if (value.Length == dateTimeFormat.Length && DateTime.TryParseExact(value, dateTimeFormat, CultureInfo.InvariantCulture, DateTimeStyles.None, out checkDateTime))
dateTime = checkDateTime;
else
dateTime = GetDateTime(dateTimeFormat, value);
}
if (image.PropertyIdList.Contains((int)IExif.Tags.DateTimeDigitized))
{
propertyItem = image.GetPropertyItem((int)IExif.Tags.DateTimeDigitized);
if (propertyItem?.Value is not null)
{
value = asciiEncoding.GetString(propertyItem.Value, 0, propertyItem.Len - 1);
if (value.Length > dateTimeFormat.Length)
value = value[..dateTimeFormat.Length];
if (value.Length == dateTimeFormat.Length && DateTime.TryParseExact(value, dateTimeFormat, CultureInfo.InvariantCulture, DateTimeStyles.None, out checkDateTime))
dateTimeDigitized = checkDateTime;
else
dateTimeDigitized = GetDateTime(dateTimeFormat, value);
}
}
if (image.PropertyIdList.Contains((int)IExif.Tags.DateTimeOriginal))
{
propertyItem = image.GetPropertyItem((int)IExif.Tags.DateTimeOriginal);
if (propertyItem?.Value is not null)
{
value = asciiEncoding.GetString(propertyItem.Value, 0, propertyItem.Len - 1);
if (value.Length > dateTimeFormat.Length)
value = value[..dateTimeFormat.Length];
if (value.Length == dateTimeFormat.Length && DateTime.TryParseExact(value, dateTimeFormat, CultureInfo.InvariantCulture, DateTimeStyles.None, out checkDateTime))
dateTimeOriginal = checkDateTime;
else
dateTimeOriginal = GetDateTime(dateTimeFormat, value);
}
}
if (image.PropertyIdList.Contains((int)IExif.Tags.GPSDateStamp))
{
propertyItem = image.GetPropertyItem((int)IExif.Tags.GPSDateStamp);
if (propertyItem?.Value is not null)
{
value = asciiEncoding.GetString(propertyItem.Value, 0, propertyItem.Len - 1);
if (value.Length > dateTimeFormat.Length)
value = value[..dateTimeFormat.Length];
if (value.Length == dateTimeFormat.Length && DateTime.TryParseExact(value, dateTimeFormat, CultureInfo.InvariantCulture, DateTimeStyles.None, out checkDateTime))
gpsDateStamp = checkDateTime;
else
gpsDateStamp = GetDateTime(dateTimeFormat, value);
}
}
image.Dispose();
}
}
catch (Exception)
{
message = string.Concat(new StackFrame().GetMethod()?.Name, " <", fileHolder.FullName, ">");
}
}
DateTime?[] dateTimes = new DateTime?[] { fileHolder.LastWriteTime, fileHolder.CreationTime, dateTime, dateTimeDigitized, dateTimeOriginal, gpsDateStamp };
return new(dateTimeOriginal, dateTimes, id, message);
}
internal static PropertyItem GetPropertyItem(ConstructorInfo constructorInfo, int id, short type, string value)
{
PropertyItem result = (PropertyItem)constructorInfo.Invoke(null);
int length;
byte[] bytes;
if (type == 2)
{
bytes = GetBytes(value);
length = value.Length + 1;
}
else if (type == 1)
{
bytes = Encoding.Unicode.GetBytes($"{value}\0");
length = bytes.Length;
}
else
throw new NotSupportedException();
result.Id = id;
result.Len = length;
result.Type = type;
result.Value = bytes;
return result;
}
#pragma warning restore CA1416
internal static DateTime? GetDateTime(string dateTimeFormat, string? value)
{
DateTime? result;
string alternateFormat = "ddd MMM dd HH:mm:ss yyyy";
if (value is not null && DateTime.TryParse(value, out DateTime dateTime))
result = dateTime;
else if (value is not null && value.Length == dateTimeFormat.Length && DateTime.TryParseExact(value, dateTimeFormat, CultureInfo.InvariantCulture, DateTimeStyles.None, out dateTime))
result = dateTime;
else if (value is not null && value.Length == alternateFormat.Length && DateTime.TryParseExact(value, alternateFormat, CultureInfo.InvariantCulture, DateTimeStyles.None, out dateTime))
result = dateTime;
else
result = null;
return result;
}
internal static byte[] GetBytes(string value)
{
byte[] results = new byte[value.Length + 1];
for (int i = 0; i < value.Length; i++)
results[i] = (byte)value[i];
results[value.Length] = 0x00;
return results;
}
}

View File

@ -3,7 +3,7 @@ namespace View_by_Distance.Shared.Models.Stateless.Methods;
internal abstract partial class XDirectory
{
private record SortedRecord(int? Id, Models.FileHolder FileHolder);
private record SortedRecord(Models.FileHolder FileHolder, bool NameWithoutExtensionIsIdFormat, int? Id);
private static int GetCeilingAverage(List<string[]> fileCollection)
{
@ -244,7 +244,7 @@ internal abstract partial class XDirectory
else if (fromFileInfo.Length == toFileInfo.Length && fromFileInfo.LastWriteTime == toFileInfo.LastWriteTime)
checkDirectory = string.Concat(checkDirectory, ".del");
else
checkDirectory = string.Concat(checkDirectory, ".i");
checkDirectory = string.Concat(checkDirectory, ".j");
}
File.Move(from, checkDirectory);
}
@ -280,79 +280,91 @@ internal abstract partial class XDirectory
}
}
private static (bool, SortedRecord[]) GetSortedRecords(List<string[]> filesCollection)
private static SortedRecord[] GetSortedRecords(List<string[]> filesCollection)
{
bool result = true;
List<SortedRecord> results = new();
int? id;
short? multiplier;
char negativeMarker;
int absoluteValueOfId;
Models.FileHolder fileHolder;
int offset = IDirectory.GetOffset();
int offsetLength = offset.ToString().Length;
int sortOrderOnlyLengthIndex = offsetLength + 3;
bool nameWithoutExtensionIsIdFormat;
bool nameWithoutExtensionIsPaddedIdFormat;
int sortOrderOnlyLengthIndex = IDirectory.GetSortOrderOnlyLengthIndex();
foreach (string[] files in filesCollection)
{
foreach (string file in files)
{
fileHolder = new Models.FileHolder(file);
if (!result)
nameWithoutExtensionIsIdFormat = IProperty.NameWithoutExtensionIsIdFormat(fileHolder);
nameWithoutExtensionIsPaddedIdFormat = IDirectory.NameWithoutExtensionIsPaddedIdFormat(fileHolder, sortOrderOnlyLengthIndex);
if (nameWithoutExtensionIsIdFormat)
{
multiplier = null;
if (!int.TryParse(fileHolder.NameWithoutExtension, out absoluteValueOfId))
id = null;
else
id = absoluteValueOfId;
}
else if (!nameWithoutExtensionIsPaddedIdFormat)
{
id = null;
multiplier = null;
}
else
{
if (fileHolder.NameWithoutExtension.Length < sortOrderOnlyLengthIndex)
{
result = false;
negativeMarker = fileHolder.NameWithoutExtension[sortOrderOnlyLengthIndex - 2];
if (negativeMarker == '7')
multiplier = 1;
else if (negativeMarker == '3')
multiplier = -1;
else
multiplier = null;
}
if (!int.TryParse(fileHolder.NameWithoutExtension[sortOrderOnlyLengthIndex..], out absoluteValueOfId))
id = null;
else
{
negativeMarker = fileHolder.NameWithoutExtension[sortOrderOnlyLengthIndex - 2];
if (negativeMarker == '7')
multiplier = 1;
else if (negativeMarker == '3')
multiplier = -1;
else
{
result = false;
multiplier = null;
}
id = absoluteValueOfId * multiplier;
if (id is null || !fileHolder.NameWithoutExtension.EndsWith(id.Value.ToString()[1..]))
id = null;
}
}
if (!result)
id = null;
else if (!int.TryParse(fileHolder.NameWithoutExtension[sortOrderOnlyLengthIndex..], out absoluteValueOfId))
id = null;
else
id = absoluteValueOfId * multiplier;
results.Add(new(id, fileHolder));
results.Add(new(fileHolder, nameWithoutExtensionIsIdFormat, id));
}
}
return new(result, (from l in results orderby l.FileHolder.LastWriteTime, l.FileHolder.FullName.Length descending select l).ToArray());
return (from l in results orderby l.FileHolder.CreationTime, l.FileHolder.FullName.Length descending select l).ToArray();
}
internal static (string[], List<(Models.FileHolder, string)>) GetToDoCollection(Properties.IPropertyConfiguration propertyConfiguration, bool copyDuplicates, bool ifCanUseId, List<string[]> filesCollection, string[] directories, Action? tick)
{
List<(Models.FileHolder, string)> results = new();
string paddedId;
string checkFile;
string directory;
FileInfo fileInfo;
int directoryIndex;
string directoryName;
string paddedIdFile;
bool wrapped = false;
string directoryName;
bool paddedCheck = false;
SortedRecord sortedRecord;
Models.FileHolder fileHolder;
List<int> distinctIds = new();
List<string> distinct = new();
int offset = IDirectory.GetOffset();
List<string> distinctDirectories = new();
(bool all, SortedRecord[] sortedRecords) = GetSortedRecords(filesCollection);
foreach (SortedRecord sortedRecord in sortedRecords)
int intMinValueLength = int.MinValue.ToString().Length;
SortedRecord[] sortedRecords = GetSortedRecords(filesCollection);
for (int i = 0; i < sortedRecords.Length; i++)
{
tick?.Invoke();
if (sortedRecord.FileHolder.Name.EndsWith("len") || sortedRecord.FileHolder.ExtensionLowered == ".id" || sortedRecord.FileHolder.ExtensionLowered == ".lsv" || sortedRecord.FileHolder.DirectoryName is null)
sortedRecord = sortedRecords[i];
fileHolder = sortedRecord.FileHolder;
if (fileHolder.Name.EndsWith("len") || fileHolder.ExtensionLowered == ".id" || fileHolder.ExtensionLowered == ".lsv" || fileHolder.DirectoryName is null)
continue;
(_, directoryIndex) = IPath.GetDirectoryNameAndIndex(propertyConfiguration.ResultAllInOneSubdirectoryLength, sortedRecord.FileHolder.NameWithoutExtension);
directoryName = Path.GetFileName(sortedRecord.FileHolder.DirectoryName);
if (directoryName.Length < propertyConfiguration.ResultAllInOneSubdirectoryLength + 3 || !sortedRecord.FileHolder.Name.StartsWith(directoryName))
(_, directoryIndex) = IPath.GetDirectoryNameAndIndex(propertyConfiguration.ResultAllInOneSubdirectoryLength, fileHolder.NameWithoutExtension);
directoryName = Path.GetFileName(fileHolder.DirectoryName);
if (directoryName.Length < propertyConfiguration.ResultAllInOneSubdirectoryLength + 3 || !fileHolder.Name.StartsWith(directoryName))
{
if (wrapped)
continue;
@ -364,28 +376,35 @@ internal abstract partial class XDirectory
wrapped = true;
directory = Path.Combine(directories[directoryIndex], directoryName);
}
if (all)
if (ifCanUseId && sortedRecord.NameWithoutExtensionIsIdFormat && sortedRecord.Id is not null && fileHolder.DirectoryName is not null)
{
if (sortedRecord.Id is null || !sortedRecord.FileHolder.NameWithoutExtension.EndsWith(sortedRecord.Id.Value.ToString()[1..]))
throw new NotSupportedException();
paddedId = IDirectory.GetPaddedId(intMinValueLength, offset + i, sortedRecord.Id.Value);
paddedIdFile = Path.Combine(fileHolder.DirectoryName, $"{paddedId}{fileHolder.ExtensionLowered}");
if (!File.Exists(paddedIdFile))
{
File.Move(fileHolder.FullName, paddedIdFile);
fileHolder = new(paddedIdFile);
if (!paddedCheck)
paddedCheck = true;
}
}
if (all && ifCanUseId)
checkFile = Path.Combine(directory, $"{sortedRecord.Id}{sortedRecord.FileHolder.ExtensionLowered}");
if (ifCanUseId)
checkFile = Path.Combine(directory, $"{sortedRecord.Id}{fileHolder.ExtensionLowered}");
else
checkFile = Path.Combine(directory, $"{sortedRecord.FileHolder.NameWithoutExtension}{sortedRecord.FileHolder.ExtensionLowered}");
checkFile = Path.Combine(directory, $"{fileHolder.NameWithoutExtension}{fileHolder.ExtensionLowered}");
if ((sortedRecord.Id is not null && distinctIds.Contains(sortedRecord.Id.Value)) || distinct.Contains(checkFile))
{
if (string.IsNullOrEmpty(sortedRecord.FileHolder.DirectoryName))
if (string.IsNullOrEmpty(fileHolder.DirectoryName))
continue;
if (!copyDuplicates)
continue;
for (int i = 1; i < int.MaxValue; i++)
for (int j = 1; j < int.MaxValue; j++)
{
fileInfo = new(checkFile);
if (!fileInfo.Exists || sortedRecord.FileHolder.Length == fileInfo.Length && sortedRecord.FileHolder.LastWriteTime == fileInfo.LastWriteTime)
checkFile = Path.Combine(directory, $"{sortedRecord.FileHolder.NameWithoutExtension}.{i}dup{sortedRecord.FileHolder.ExtensionLowered}");
if (!fileInfo.Exists || fileHolder.Length == fileInfo.Length && fileHolder.LastWriteTime == fileInfo.LastWriteTime)
checkFile = Path.Combine(directory, $"{fileHolder.NameWithoutExtension}.{j}dup{fileHolder.ExtensionLowered}");
else
checkFile = Path.Combine(directory, $"{sortedRecord.FileHolder.NameWithoutExtension}.{i}why{sortedRecord.FileHolder.ExtensionLowered}");
checkFile = Path.Combine(directory, $"{fileHolder.NameWithoutExtension}.{j}why{fileHolder.ExtensionLowered}");
if (sortedRecord.Id is not null)
{
if (distinctIds.Contains(sortedRecord.Id.Value))
@ -395,7 +414,7 @@ internal abstract partial class XDirectory
if (distinct.Contains(checkFile))
continue;
distinct.Add(checkFile);
results.Add(new(sortedRecord.FileHolder, checkFile));
results.Add(new(fileHolder, checkFile));
if (!distinctDirectories.Contains(directory))
distinctDirectories.Add(directory);
break;
@ -405,10 +424,12 @@ internal abstract partial class XDirectory
distinct.Add(checkFile);
if (sortedRecord.Id is not null)
distinctIds.Add(sortedRecord.Id.Value);
results.Add(new(sortedRecord.FileHolder, checkFile));
results.Add(new(fileHolder, checkFile));
if (!distinctDirectories.Contains(directory))
distinctDirectories.Add(directory);
}
if (paddedCheck)
throw new Exception("Maybe need to restart application!");
return (distinctDirectories.ToArray(), results);
}