Skip to content

Commit

Permalink
Switches to lower case extensions. Fixes duplicate file handling for …
Browse files Browse the repository at this point in the history
…new file names. Allows rename to happen when file is already in the correct folder. Version 0.2.1.
  • Loading branch information
bernarden committed May 24, 2024
1 parent 93a9e33 commit 25836bd
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 12 deletions.
13 changes: 8 additions & 5 deletions Source/Vima.MediaSorter/Helpers/FileMovingHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ public enum MoveStatus
Duplicate
}

public static (MoveStatus, string?) MoveFile(string filePath, string destinationFolderPath, string destinationFileName)
public static (MoveStatus, string?) MoveFile(
string filePath, string destinationFolderPath, string destinationFileName)
{
if (!PreviouslyCreatedFolders.Contains(destinationFolderPath))
{
Expand All @@ -29,21 +30,23 @@ public static (MoveStatus, string?) MoveFile(string filePath, string destination
return (MoveStatus.Duplicate, filePathInNewFolder);
}

filePathInNewFolder = GenerateUniqueName(filePath, destinationFolderPath);
filePathInNewFolder = GenerateUniqueName(destinationFolderPath, destinationFileName);
}

File.Move(filePath, filePathInNewFolder);
return (MoveStatus.Success, null);
}

private static string GenerateUniqueName(string filePath, string newFolderPath)
private static string GenerateUniqueName(string newFolderPath, string destinationFileName)
{
string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(destinationFileName);
string extension = Path.GetExtension(destinationFileName).ToLowerInvariant();

string filePathInNewFolder;
int count = 1;
do
{
string fileName =
$"{Path.GetFileNameWithoutExtension(filePath)} ({count}){Path.GetExtension(filePath)}";
string fileName = $"{fileNameWithoutExtension} ({count}){extension}";
filePathInNewFolder = Path.Combine(newFolderPath, fileName);
count++;
} while (File.Exists(filePathInNewFolder));
Expand Down
12 changes: 6 additions & 6 deletions Source/Vima.MediaSorter/MediaFileProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ namespace Vima.MediaSorter;
public class MediaFileProcessor(string sourceDirectory)
{
public static readonly string FolderNameFormat = "yyyy_MM_dd -";
public static readonly List<string> ImageExtensions = [".JPG", ".JPEG"];
public static readonly List<string> VideoExtensions = [".MP4"];
public static readonly List<string> ImageExtensions = [".jpg", ".jpeg"];
public static readonly List<string> VideoExtensions = [".mp4"];
private readonly Dictionary<DateTime, string> _dateToExistingDirectoryMapping = [];
private readonly List<DuplicateFile> _duplicateFiles = [];
private readonly List<string> _ignoredFiles = [];
Expand Down Expand Up @@ -75,13 +75,13 @@ private List<MediaFile> IdentifyMediaFiles()
int processedFileCounter = 0;
Parallel.ForEach(filePaths, new() { MaxDegreeOfParallelism = 25 }, filePath =>
{
if (ImageExtensions.Contains(Path.GetExtension(filePath).ToUpperInvariant()))
if (ImageExtensions.Contains(Path.GetExtension(filePath).ToLowerInvariant()))
{
MediaFile mediaFile = new(filePath, MediaFileType.Image);
MediaMetadataHelper.SetCreatedDateTime(mediaFile);
mediaFiles.Add(mediaFile);
}
else if (VideoExtensions.Contains(Path.GetExtension(filePath).ToUpperInvariant()))
else if (VideoExtensions.Contains(Path.GetExtension(filePath).ToLowerInvariant()))
{
MediaFile mediaFile = new(filePath, MediaFileType.Video);
IEnumerable<string> relatedFiles = RelatedFilesHelper.FindAll(filePath);
Expand Down Expand Up @@ -185,11 +185,11 @@ private void MoveFile(string filePath, DateTime createdDateTime, bool shouldRena
}

string destinationFileName = shouldRenameFile
? createdDateTime.ToString("yyyyMMdd_HHmmss") + Path.GetExtension(filePath)
? createdDateTime.ToString("yyyyMMdd_HHmmss") + Path.GetExtension(filePath).ToLowerInvariant()
: Path.GetFileName(filePath);

string destinationFolderPath = Path.Combine(sourceDirectory, directoryName);
if (filePath.StartsWith(destinationFolderPath)) return;
if (filePath.StartsWith(destinationFolderPath) && !shouldRenameFile) return;

(FileMovingHelper.MoveStatus status, string? destinationPath) =
FileMovingHelper.MoveFile(filePath, destinationFolderPath, destinationFileName);
Expand Down
2 changes: 1 addition & 1 deletion Source/Vima.MediaSorter/Vima.MediaSorter.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<ApplicationIcon>Resources/icon.ico</ApplicationIcon>
<Nullable>enable</Nullable>
<OutputType>Exe</OutputType>
<AssemblyVersion>0.2.0</AssemblyVersion>
<AssemblyVersion>0.2.1</AssemblyVersion>
</PropertyGroup>

<ItemGroup>
Expand Down

0 comments on commit 25836bd

Please sign in to comment.