public DirectoryScanResult ScanDirectory(string directoryPath) { var filesToScan = _getFilesToProcess(directoryPath); Console.WriteLine($"Found {filesToScan.Count} files to process."); var result = new DirectoryScanResult(); foreach (var file in filesToScan) { try { var directories = ImageMetadataReader.ReadMetadata(file); var subIfdDirectory = directories.OfType <ExifSubIfdDirectory>().FirstOrDefault(); var dateTakenText = subIfdDirectory?.Tags?.FirstOrDefault(x => string.Equals(x.Name, ExifDateTimeTagName, StringComparison.OrdinalIgnoreCase))?.Description; DateTime?parsedDateTime = ExifDateTimeParser.Parse(dateTakenText); if (parsedDateTime.HasValue) { if (!result.StartingDate.HasValue || result.StartingDate > parsedDateTime.Value) { result.StartingDate = parsedDateTime.Value; } if (!result.EndingDate.HasValue || result.EndingDate < parsedDateTime.Value) { result.EndingDate = parsedDateTime.Value; } } } catch (ImageProcessingException ex) { Console.WriteLine($"Error while processing the file '{file}': {ex.Message}"); } } return(result); }
public void UpdateDirectoryName(string directoryPath, DirectoryScanResult scanResult) { var directoryName = new DirectoryInfo(directoryPath).Name; if (scanResult.StartingDate.HasValue && scanResult.EndingDate.HasValue) { // start and end dates are set if (scanResult.StartingDate.Value.Year != scanResult.EndingDate.Value.Year) { Console.WriteLine("Detected a directory that has pictures taken in different years, skipping."); return; } string suggestedDirectoryName; if (scanResult.StartingDate.Value.Month != scanResult.EndingDate.Value.Month) { suggestedDirectoryName = $"{scanResult.StartingDate.Value.ToString("yyyy-MM-dd")}-{scanResult.EndingDate.Value.ToString("yyyy-MM-dd")} {directoryName}"; } else if (scanResult.StartingDate.Value.Day != scanResult.EndingDate.Value.Day) { suggestedDirectoryName = $"{scanResult.StartingDate.Value.ToString("yyyy-MM-dd")}-{scanResult.EndingDate.Value.ToString("yyyy-MM-dd")} {directoryName}"; } else { suggestedDirectoryName = $"{scanResult.StartingDate.Value.ToString("yyyy-MM-dd")} {directoryName}"; } Console.WriteLine($"Changing {directoryName} -> {suggestedDirectoryName}"); Directory.Move(directoryPath, directoryPath.Replace(directoryName, suggestedDirectoryName)); } else { Console.WriteLine($"Directory {directoryName} did not return any scan result, no need to change its name."); } }