Add project files.
All checks were successful
continuous-integration/appveyor/branch AppVeyor build succeeded

This commit is contained in:
Brychan Dempsey 2021-03-12 18:07:52 +13:00
parent 336eebe923
commit 22deadd4b8
5 changed files with 484 additions and 0 deletions

25
Image Sorter.sln Normal file
View File

@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.29230.47
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Image Sorter", "Image Sorter\Image Sorter.csproj", "{6739EA9D-6361-4B5B-B687-07C30FB82B3B}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{6739EA9D-6361-4B5B-B687-07C30FB82B3B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6739EA9D-6361-4B5B-B687-07C30FB82B3B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6739EA9D-6361-4B5B-B687-07C30FB82B3B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6739EA9D-6361-4B5B-B687-07C30FB82B3B}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {55B297F0-6CDC-42E4-A9E1-73EA759B867B}
EndGlobalSection
EndGlobal

6
Image Sorter/App.config Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
</startup>
</configuration>

View File

@ -0,0 +1,82 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{6739EA9D-6361-4B5B-B687-07C30FB82B3B}</ProjectGuid>
<OutputType>Exe</OutputType>
<RootNamespace>Image_Sorter</RootNamespace>
<AssemblyName>Image Sorter</AssemblyName>
<TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<Deterministic>true</Deterministic>
<PublishUrl>publish\</PublishUrl>
<Install>true</Install>
<InstallFrom>Disk</InstallFrom>
<UpdateEnabled>false</UpdateEnabled>
<UpdateMode>Foreground</UpdateMode>
<UpdateInterval>7</UpdateInterval>
<UpdateIntervalUnits>Days</UpdateIntervalUnits>
<UpdatePeriodically>false</UpdatePeriodically>
<UpdateRequired>false</UpdateRequired>
<MapFileExtensions>true</MapFileExtensions>
<ApplicationRevision>0</ApplicationRevision>
<ApplicationVersion>1.0.0.%2a</ApplicationVersion>
<IsWebBootstrapper>false</IsWebBootstrapper>
<UseApplicationTrust>false</UseApplicationTrust>
<BootstrapperEnabled>true</BootstrapperEnabled>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="PresentationCore" />
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
<Reference Include="WindowsBase" />
</ItemGroup>
<ItemGroup>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
</ItemGroup>
<ItemGroup>
<BootstrapperPackage Include=".NETFramework,Version=v4.7.2">
<Visible>False</Visible>
<ProductName>Microsoft .NET Framework 4.7.2 %28x86 and x64%29</ProductName>
<Install>true</Install>
</BootstrapperPackage>
<BootstrapperPackage Include="Microsoft.Net.Framework.3.5.SP1">
<Visible>False</Visible>
<ProductName>.NET Framework 3.5 SP1</ProductName>
<Install>false</Install>
</BootstrapperPackage>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>

335
Image Sorter/Program.cs Normal file
View File

@ -0,0 +1,335 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media.Imaging;
using System.Globalization;
using System.Threading;
namespace Image_Sorter
{
class Program
{
static readonly List<string> DNGTypes = new List<string> {"raw", "cr2", "dng"};
static readonly List<string> ImagePrefixes = new List<string> { "", "Screenshot_", "VID"};
static void Main(string[] args)
{
Console.WriteLine("Image Sorter {0} (C)2019 Brychan Dempsey");
Console.WriteLine("Moves images from the source folder and arranges them by date (from metadata) in the destination folder");
string SourceDir = "";
string DestinationDir = "";
bool copyFiles = false;
if (args.Length > 0 && args[0].ToLower() == "\\s")
{
int sourceDirSpecified = Array.IndexOf(args, "\\d");
if (sourceDirSpecified != -1)
{
if (Directory.Exists(args[sourceDirSpecified + 1]))
{
SourceDir = args[sourceDirSpecified + 1];
}
}
int targetDirSpecified = Array.IndexOf(args, "\\t");
if (targetDirSpecified != -1)
{
if (Directory.Exists(args[targetDirSpecified + 1]))
{
DestinationDir = args[targetDirSpecified + 1];
}
}
else
{
DestinationDir = SourceDir;
}
if (args.Contains("\\c"))
{
copyFiles = true;
}
// Redirect all console writes
Console.SetOut(new StringWriter());
}
else if (args.Length > 0 && (args[0] == "\\?" || args[0].ToLower() == "\\h"))
{
Console.WriteLine("Image Sorter");
Console.WriteLine("Can be run in silent mode using the argument: \\s");
Console.WriteLine("Additional flags: \\d - Source Directory (Usage: \\s \\d <soruce directory>");
Console.WriteLine("\t\t\\t - Target Directory (Usage: \\s \\t <target directory>");
Console.WriteLine("\t\t\\c - Copy files if this flag is specified");
Console.WriteLine("Usage Example:");
Console.WriteLine("\\s \\d \"C:\\Camera Files\\Images\" \\t \"C:\\Camera Files\\Sorted\\\"");
Console.WriteLine("\nIf the destination directory is not specified, it will default to the source directory");
Console.ReadLine();
Environment.Exit(0);
}
else
{
Console.WriteLine("Enter the source directory:");
SourceDir = Console.ReadLine();
Console.WriteLine("Enter the destionation directory:");
DestinationDir = Console.ReadLine();
Console.WriteLine("Enter \'y\' to perform a copy instead of move");
string tRead = Console.ReadLine();
if (tRead.Trim().ToLower().Equals("y"))
{
copyFiles = true;
}
}
Console.WriteLine("Scanning the source directory...");
List<string> SourceFiles = GetFiles(SourceDir);
Console.WriteLine("Found {0} files", SourceFiles.Count());
int processedCount = 0;
object countLock = new object();
Task updateFilesTask = new Task(() =>
{
Parallel.ForEach(SourceFiles, (filepath) =>
{
string fileDestination = "";
DateTime photoDate = new DateTime();
bool hasMetadata = true;
using (FileStream fs = new FileStream(filepath, FileMode.Open, FileAccess.Read, FileShare.Read))
{
bool ruleMatch = false;
Dictionary<string, string> imgMetadata = GetMetaData(fs);
if (imgMetadata.ContainsKey("date"))
{
photoDate = DateTime.Parse(imgMetadata["date"]);
ruleMatch = true;
}
// If the file is a negative, it may have a conjugate image file
else if (DNGTypes.Contains(Path.GetExtension(filepath).TrimStart('.')))
{
List<string> matches = SourceFiles.FindAll((x) => Path.GetFileNameWithoutExtension(x).Equals(Path.GetFileNameWithoutExtension(filepath)) && !Path.GetExtension(x).Equals(Path.GetExtension(filepath)));
if (matches.Count == 1)
{
using (FileStream tfs = new FileStream(matches[0], FileMode.Open, FileAccess.Read, FileShare.Read))
{
imgMetadata = GetMetaData(tfs);
}
if (imgMetadata.ContainsKey("date"))
{
photoDate = DateTime.Parse(imgMetadata["date"]);
Console.WriteLine(" Conjugate file found - " + Path.GetFileName(filepath));
ruleMatch = true;
}
}
}
// Try parsing a date by the rules set in ImagePrefixes, using the first if found
else if (ImagePrefixes.Exists((x) => TryParseDate(Path.GetFileNameWithoutExtension(filepath).TrimStart(x.ToCharArray()), out photoDate)))
{
Console.WriteLine(" Date successfully parsed from file: {0} - Parsed Date: {1} ", Path.GetFileName(filepath), photoDate.ToString("dd-MM-yyyy"));
ruleMatch = true;
}
// Failsafe to avoid implausable dates (Greater than system year + 10, < 1990)
if (ruleMatch && (photoDate.Year > DateTime.Now.Year + 10 || photoDate.Year < 1990))
{
ruleMatch = false;
}
// Finally, resort to the least of the file creation time && file modified time
if (!ruleMatch)
{
FileSystemInfo fileInfo = new FileInfo(filepath);
photoDate = fileInfo.CreationTime < fileInfo.LastWriteTime ? fileInfo.CreationTime : fileInfo.LastWriteTime;
}
}
fileDestination = "\\" + photoDate.Year;
fileDestination += "\\" + CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(photoDate.Month);
Directory.CreateDirectory(DestinationDir + fileDestination + "\\");
fileDestination += "\\" + filepath.Remove(0, filepath.LastIndexOf("\\"));
if (copyFiles)
{
try
{
File.Copy(filepath, DestinationDir + fileDestination);
filepath = DestinationDir + fileDestination;
}
catch
{
Console.WriteLine("Failed to copy {0}", Path.GetFileName(filepath));
}
}
else
{
try
{
File.Move(filepath, DestinationDir + fileDestination);
filepath = DestinationDir + fileDestination;
}
catch
{
Console.WriteLine("Failed to move {0}", Path.GetFileName(filepath));
}
}
lock (countLock)
{
processedCount++;
}
});
});
int lastProcessed = 0;
updateFilesTask.Start();
bool eval = false;
while (!eval)
{
lock (countLock)
{
if (processedCount != lastProcessed)
{
Console.WriteLine("{0}/{1} processed.", processedCount, SourceFiles.Count);
lastProcessed = processedCount;
}
if (processedCount == SourceFiles.Count)
eval = true;
}
Thread.Sleep(10);
}
Console.ReadLine();
}
static Dictionary<string,string> GetMetaData(FileStream fileStream)
{
Dictionary<string, string> Metadata = new Dictionary<string, string>();
try
{
BitmapSource srcimg = BitmapFrame.Create(fileStream);
BitmapMetadata md = (BitmapMetadata)srcimg.Metadata;
string date = md.DateTaken;
if (!string.IsNullOrEmpty(date))
{
Metadata.Add("date", date);
}
}
catch
{
Console.WriteLine("Couldn't get metadata - {0}", fileStream.Name);
}
return Metadata;
}
static List<string> GetFiles(string SourceDirectory)
{
List<string> foundFiles = Directory.GetFiles(SourceDirectory, "*", SearchOption.TopDirectoryOnly).ToList();
foreach (var subDirectory in Directory.GetDirectories(SourceDirectory))
{
foundFiles.AddRange(GetFiles(subDirectory));
}
return foundFiles;
}
static bool TryParseDate(string input, out DateTime dt)
{
// Attempt a base conversion first
// As datetime's built-in converter does not seem to be able to handle more complex strings,
// convert the to a usable format before attempting to convert a datetime
//int pos = ImagePrefixes.FindIndex(0, (x) => input.StartsWith(x));
//input = input.Replace(ImagePrefixes[pos], ""); // Remove the prefix
if (DateTime.TryParse(input, out dt))
return true;
StringBuilder converted = new StringBuilder();
string modified = input;
DateTime result = new DateTime();
// if we are in the format "ddddsddsdd" we can try to parse that date
if (isMatch(input))
{
int year = Convert.ToInt32(input.Substring(0, 4));
int month = Convert.ToInt32(input.Substring(5, 2));
int day = Convert.ToInt32(input.Substring(8, 2));
try
{
result = new DateTime(year, month, day);
}
catch
{
return false;
}
}
// Else try the straight digit parsing, so ensure we have 8 contiguous digits to parse.
else if (isMatch(input, "dddddddd"))
{
int year = Convert.ToInt32(input.Substring(0, 4));
int month = Convert.ToInt32(input.Substring(4, 2));
int day = Convert.ToInt32(input.Substring(6, 2));
try
{
result = new DateTime(year, month, day);
}
catch
{
return false;
}
}
if (result.Year <= DateTime.Now.Year+10 && result.Year >= 1980)
{
// Falls within an acceptable date range, so try use it as a date
dt = result;
return true;
}
return false;
}
private static bool isNumeric(char c1)
{
if (c1 >= 48 && c1 < 58)
{
return true;
}
else return false;
}
private static bool isSymbol(char c1)
{
if ((c1 >= 32 && c1 < 48) || (c1 >= 58 && c1 < 65) || (c1 >= 91 && c1 < 97) || (c1 >= 123 && c1 < 127))
{
return true;
}
else return false;
}
/// <summary>
/// Checks if the provided string matches the format specified
/// d = digit
/// a = alphanumeric
/// s = symbol
/// * = any
/// </summary>
/// <param name="source"></param>
/// <param name="format"></param>
/// <returns></returns>
private static bool isMatch(string source, string format = "ddddsddsdd")
{
if (source.Length < format.Length) return false;
bool state = true;
for (int i = 0; i < format.Length; i++)
{
if (format[i] == 'd')
{
if (!isNumeric(source[i]))
{
return false;
}
}
else if (format[i] == 's')
{
if (!isSymbol(source[i]))
{
return false;
}
}
}
return state;
}
}
}

View File

@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("Image Sorter")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("Image Sorter")]
[assembly: AssemblyCopyright("Copyright © 2019")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]
// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("6739ea9d-6361-4b5b-b687-07c30fb82b3b")]
// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]