Get lastAccessTime from file
This commit is contained in:
parent
64a14f39c1
commit
a8e89b841e
@ -1,25 +1,25 @@
|
|||||||
using System;
|
using MetadataExtractor;
|
||||||
|
using MetadataExtractor.Formats.Exif;
|
||||||
|
using MetadataExtractor.Formats.QuickTime;
|
||||||
|
using Microsoft.Extensions.Configuration;
|
||||||
|
using Serilog;
|
||||||
|
using ShellProgressBar;
|
||||||
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Net;
|
using System.Net;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using MetadataExtractor;
|
|
||||||
using MetadataExtractor.Formats.Exif;
|
|
||||||
using MetadataExtractor.Formats.QuickTime;
|
|
||||||
using Microsoft.Extensions.Configuration;
|
|
||||||
using Serilog;
|
|
||||||
using ShellProgressBar;
|
|
||||||
using Directory = System.IO.Directory;
|
using Directory = System.IO.Directory;
|
||||||
|
|
||||||
namespace PhotoRenamer
|
namespace PhotoRenamer
|
||||||
{
|
{
|
||||||
internal class Renamer
|
internal class Renamer
|
||||||
{
|
{
|
||||||
|
private readonly ProgressBarOptions _childOptions;
|
||||||
private readonly string _sourcePath;
|
private readonly string _sourcePath;
|
||||||
private readonly string _targetPath;
|
private readonly string _targetPath;
|
||||||
private readonly ProgressBarOptions _childOptions;
|
|
||||||
|
|
||||||
public Renamer(IConfiguration configuration)
|
public Renamer(IConfiguration configuration)
|
||||||
{
|
{
|
||||||
@ -48,13 +48,15 @@ namespace PhotoRenamer
|
|||||||
};
|
};
|
||||||
var i = 0;
|
var i = 0;
|
||||||
using var progressBar = new ProgressBar(files.Length, "Copying files", options);
|
using var progressBar = new ProgressBar(files.Length, "Copying files", options);
|
||||||
var po = new ParallelOptions {MaxDegreeOfParallelism = 4};
|
var po = new ParallelOptions { MaxDegreeOfParallelism = 4 };
|
||||||
Parallel.ForEach(files, po, file =>
|
Parallel.ForEach(files, po, file =>
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var directories = ImageMetadataReader.ReadMetadata(file);
|
var directories = ImageMetadataReader.ReadMetadata(file);
|
||||||
var dateTime = GetDateTimeFromExif(directories) ?? GetDateTimeFromMp4(directories);
|
var dateTime = GetDateTimeFromExif(directories)
|
||||||
|
?? GetDateTimeFromMp4(directories)
|
||||||
|
?? GetDateTimeFromCreate(file);
|
||||||
if (dateTime is null) return;
|
if (dateTime is null) return;
|
||||||
var folder = CreateFolder(dateTime.GetValueOrDefault());
|
var folder = CreateFolder(dateTime.GetValueOrDefault());
|
||||||
CopyFile(folder, file, progressBar);
|
CopyFile(folder, file, progressBar);
|
||||||
@ -73,6 +75,42 @@ namespace PhotoRenamer
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static DateTime? GetDateTimeFromCreate(string file)
|
||||||
|
{
|
||||||
|
var creationTime = File.GetLastWriteTime(file);
|
||||||
|
return creationTime;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static DateTime? GetDateTimeFromExif(IEnumerable<MetadataExtractor.Directory> directories)
|
||||||
|
{
|
||||||
|
DateTime dateTime = default;
|
||||||
|
return directories
|
||||||
|
.OfType<ExifIfd0Directory>()
|
||||||
|
.FirstOrDefault()?
|
||||||
|
.TryGetDateTime(ExifDirectoryBase.TagDateTime, out dateTime) == true
|
||||||
|
? (DateTime?)dateTime
|
||||||
|
: null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static DateTime? GetDateTimeFromMp4(IEnumerable<MetadataExtractor.Directory> directories)
|
||||||
|
{
|
||||||
|
DateTime dateTime = default;
|
||||||
|
return directories
|
||||||
|
.OfType<QuickTimeMovieHeaderDirectory>()
|
||||||
|
.FirstOrDefault()?
|
||||||
|
.TryGetDateTime(QuickTimeMovieHeaderDirectory.TagCreated, out dateTime) == true
|
||||||
|
? (DateTime?)dateTime
|
||||||
|
: null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void ResetTimes(FileSystemInfo destination, FileSystemInfo source)
|
||||||
|
{
|
||||||
|
destination.LastWriteTime = source.LastWriteTime;
|
||||||
|
destination.LastWriteTimeUtc = source.LastWriteTimeUtc;
|
||||||
|
destination.CreationTime = source.CreationTime;
|
||||||
|
destination.CreationTimeUtc = source.CreationTimeUtc;
|
||||||
|
}
|
||||||
|
|
||||||
private void CopyFile(string folder, string file, ProgressBarBase progressBar)
|
private void CopyFile(string folder, string file, ProgressBarBase progressBar)
|
||||||
{
|
{
|
||||||
var destination = new FileInfo(Path.Combine(folder, Path.GetFileName(file)));
|
var destination = new FileInfo(Path.Combine(folder, Path.GetFileName(file)));
|
||||||
@ -98,41 +136,11 @@ namespace PhotoRenamer
|
|||||||
child.Tick(100);
|
child.Tick(100);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void ResetTimes(FileSystemInfo destination, FileSystemInfo source)
|
|
||||||
{
|
|
||||||
destination.LastWriteTime = source.LastWriteTime;
|
|
||||||
destination.LastWriteTimeUtc = source.LastWriteTimeUtc;
|
|
||||||
destination.CreationTime = source.CreationTime;
|
|
||||||
destination.CreationTimeUtc = source.CreationTimeUtc;
|
|
||||||
}
|
|
||||||
|
|
||||||
private string CreateFolder(DateTime dateTime)
|
private string CreateFolder(DateTime dateTime)
|
||||||
{
|
{
|
||||||
var folder = Path.Combine(_targetPath, dateTime.Year.ToString(), dateTime.Month.ToString("D2"));
|
var folder = Path.Combine(_targetPath, dateTime.Year.ToString(), dateTime.Month.ToString("D2"));
|
||||||
if (!Directory.Exists(folder)) Directory.CreateDirectory(folder);
|
if (!Directory.Exists(folder)) Directory.CreateDirectory(folder);
|
||||||
return folder;
|
return folder;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static DateTime? GetDateTimeFromExif(IEnumerable<MetadataExtractor.Directory> directories)
|
|
||||||
{
|
|
||||||
DateTime dateTime = default;
|
|
||||||
return directories
|
|
||||||
.OfType<ExifIfd0Directory>()
|
|
||||||
.FirstOrDefault()?
|
|
||||||
.TryGetDateTime(ExifDirectoryBase.TagDateTime, out dateTime) == true
|
|
||||||
? (DateTime?) dateTime
|
|
||||||
: null;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static DateTime? GetDateTimeFromMp4(IEnumerable<MetadataExtractor.Directory> directories)
|
|
||||||
{
|
|
||||||
DateTime dateTime = default;
|
|
||||||
return directories
|
|
||||||
.OfType<QuickTimeMovieHeaderDirectory>()
|
|
||||||
.FirstOrDefault()?
|
|
||||||
.TryGetDateTime(QuickTimeMovieHeaderDirectory.TagCreated, out dateTime) == true
|
|
||||||
? (DateTime?) dateTime
|
|
||||||
: null;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,4 +1,4 @@
|
|||||||
{
|
{
|
||||||
"Source": "\\\\micro\\photo\\Oneplus5",
|
"Source": "C:\\Users\\Holger\\Desktop\\2013-08-01",
|
||||||
"Target": "\\\\micro\\photo\\Oneplus5"
|
"Target": "C:\\Users\\Holger\\Desktop\\Oneplus5"
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user