From eb11d7d2db531c116e9ad670ed73e11a20128caf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Holger=20B=C3=B6rchers?= Date: Mon, 25 May 2020 21:30:17 +0200 Subject: [PATCH] first working version --- PhotoRenamer/Renamer.cs | 39 +++++++++++++++++++++-------------- PhotoRenamer/appsettings.json | 2 +- 2 files changed, 24 insertions(+), 17 deletions(-) diff --git a/PhotoRenamer/Renamer.cs b/PhotoRenamer/Renamer.cs index 75fa384..1403931 100644 --- a/PhotoRenamer/Renamer.cs +++ b/PhotoRenamer/Renamer.cs @@ -17,17 +17,17 @@ namespace PhotoRenamer private readonly string _sourcePath; private readonly string _targetPath; - public Renamer(IConfigurationRoot configuration) + public Renamer(IConfiguration configuration) { _sourcePath = Path.GetFullPath(configuration["Source"]); _targetPath = Path.GetFullPath(configuration["Target"]); - Log.Information($"Source path {_sourcePath}"); - Log.Information($"Target path {_targetPath}"); + Log.Information($"Source path: {_sourcePath}"); + Log.Information($"Target path: {_targetPath}"); } public int Run() { - var files = Directory.GetFiles(_sourcePath); + var files = Directory.GetFiles(_sourcePath, "*", SearchOption.AllDirectories); var options = new ProgressBarOptions { ForegroundColor = ConsoleColor.Yellow, @@ -39,19 +39,26 @@ namespace PhotoRenamer foreach (var file in files) { progressBar.Tick(); - var directories = ImageMetadataReader.ReadMetadata(file); + try + { + var directories = ImageMetadataReader.ReadMetadata(file); - var dateTime = GetDateTimeFromExif(directories) ?? GetDateTimeFromMp4(directories); - if (dateTime is null) continue; - var yearFolder = Path.Combine(_targetPath, dateTime.Value.Year.ToString()); - var monthFolder = Path.Combine(yearFolder, dateTime.Value.Month.ToString("D2")); - var dayFolder = Path.Combine(monthFolder, dateTime.Value.Day.ToString("D2")); - if (!Directory.Exists(yearFolder)) Directory.CreateDirectory(yearFolder); - if (!Directory.Exists(monthFolder)) Directory.CreateDirectory(monthFolder); - if (!Directory.Exists(dayFolder)) Directory.CreateDirectory(dayFolder); - var destination = Path.Combine(dayFolder, Path.GetFileName(file)); - if (File.Exists(destination)) continue; - File.Copy(file, destination); + var dateTime = GetDateTimeFromExif(directories) ?? GetDateTimeFromMp4(directories); + if (dateTime is null) continue; + var dayFolder = Path.Combine( + _targetPath, + dateTime.Value.Year.ToString(), + dateTime.Value.Month.ToString("D2"), + dateTime.Value.Day.ToString("D2")); + if (!Directory.Exists(dayFolder)) Directory.CreateDirectory(dayFolder); + var destination = Path.Combine(dayFolder, Path.GetFileName(file)); + if (File.Exists(destination)) continue; + File.Copy(file, destination); + } + catch (ImageProcessingException) + { + //silently ignore + } } return 0; diff --git a/PhotoRenamer/appsettings.json b/PhotoRenamer/appsettings.json index fbf4e3d..bc61897 100644 --- a/PhotoRenamer/appsettings.json +++ b/PhotoRenamer/appsettings.json @@ -1,4 +1,4 @@ { - "Source": "C:\\Test", + "Source": "C:\\Users\\Holger\\Nextcloud\\DCIM", "Target": "C:\\Target" } \ No newline at end of file