Rename files to padded number string
This commit is contained in:
@ -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);
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user