Skip to content

Instantly share code, notes, and snippets.

@ghyston
Created June 25, 2018 21:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ghyston/1a9d474103a1285b3c2253176f455f2d to your computer and use it in GitHub Desktop.
Save ghyston/1a9d474103a1285b3c2253176f455f2d to your computer and use it in GitHub Desktop.
PhotosMover
dotnet publish -c release --self-contained
<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>
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];
}
}
}
@ghyston
Copy link
Author

ghyston commented Jun 25, 2018

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:

  • shoot in jpg+raw, copy all in one folder
  • run script first time to put raw into separate folder
  • look at jpg's, delete those, that I don't like
  • run script second time to delete raws for bad shots

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment