Created
June 25, 2018 21:27
-
-
Save ghyston/1a9d474103a1285b3c2253176f455f2d to your computer and use it in GitHub Desktop.
PhotosMover
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
dotnet publish -c release --self-contained |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<Project Sdk="Microsoft.NET.Sdk"> | |
<PropertyGroup> | |
<OutputType>Exe</OutputType> | |
<TargetFramework>netcoreapp2.0</TargetFramework> | |
<RuntimeIdentifier>osx-x64</RuntimeIdentifier> | |
</PropertyGroup> | |
<ItemGroup> | |
<PackageReference Include="Microsoft.Extensions.FileProviders.Physical" Version="2.0.0" /> | |
</ItemGroup> | |
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.IO; | |
using System.Linq; | |
using Microsoft.Extensions.FileProviders; | |
namespace PhotosMover | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
string rawExtension = ".ORF"; | |
string[] jpgExtensions = { ".jpeg", ".jpg", ".JPG", ".JPEG" }; | |
string rawFolder = "raw"; | |
var currentDirectory = Directory.GetCurrentDirectory(); | |
IFileProvider provider = new PhysicalFileProvider(currentDirectory); | |
IDirectoryContents content = provider.GetDirectoryContents(""); | |
// Find all raw files in current directory | |
var rawFiles = content.Where(fi => fi.Name.EndsWith(rawExtension)); | |
// Check raw directory existance and, if needed, create it | |
string rawPath = $"{currentDirectory}/{rawFolder}"; | |
if(!content.Any(fi => fi.Name.Equals(rawFolder) && fi.IsDirectory) && rawFiles.Any()) | |
{ | |
Directory.CreateDirectory(rawPath); | |
Console.WriteLine($"Folder '{rawFolder}' created"); | |
} | |
// Move all raw files from current to raw directory | |
var movedCounter = 0; | |
foreach(var fileInfo in rawFiles) | |
{ | |
Directory.Move($"{currentDirectory}/{fileInfo.Name}", $"{rawPath}/{fileInfo.Name}"); | |
movedCounter++; | |
} | |
Console.WriteLine($"{movedCounter} moved to {rawPath}"); | |
// Create list of jpg files names | |
var jpgFiles = content | |
.Where(fi => jpgExtensions.Any(jpgE => fi.Name.EndsWith(jpgE))) | |
.Select(fi => cutExtension(fi)); | |
if(!jpgFiles.Any()) | |
{ | |
Console.WriteLine($"No jpg files found"); | |
return; | |
} | |
// Find raw files, that doesn't have jpg file companion | |
IDirectoryContents rawContent = provider.GetDirectoryContents(rawFolder); | |
var rawFilesDoDelete = rawContent | |
.Where(fi => { | |
var fileNameOhneExt = cutExtension(fi); | |
return fi.Name.EndsWith(rawExtension) && | |
!jpgFiles.Contains(fileNameOhneExt); | |
}); | |
// Delete solitary raw files | |
var deleteCounter = 0; | |
foreach(var i in rawFilesDoDelete) | |
{ | |
File.Delete($"{rawPath}/{i.Name}"); | |
Console.WriteLine($"File deleted: {i.Name}"); | |
deleteCounter++; | |
} | |
Console.WriteLine($"{deleteCounter} deleted"); | |
} | |
static private string cutExtension(IFileInfo fi) | |
{ | |
return fi.Name.Split(".")[0]; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Small C# script, that use LINQ to separate raw from jpg and delete raws, if there is no jpg with same name.
My photo workflow: