Skip to content

Instantly share code, notes, and snippets.

@kgabis
Created July 25, 2011 23:22
Show Gist options
  • Save kgabis/1105525 to your computer and use it in GitHub Desktop.
Save kgabis/1105525 to your computer and use it in GitHub Desktop.
RemRaw raw file remover
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace remraw
{
class Program
{
static List<FileInfo> jpglist = new List<FileInfo>();
static List<FileInfo> rawlist = new List<FileInfo>();
static List<FileInfo> todelete = new List<FileInfo>();
static void Main(string[] args)
{
Console.WriteLine("RemRaw 1.0 \nAuthor: Krzysztof Gabis (www.kzkg.pl kgabis@gmail.com)");
jpglist = GetJpegs();
rawlist = GetRaws(args.Length > 0 ? args[0] : "raws");
Console.WriteLine("Found {0} JPEG files and {1} RAW files.", jpglist.Count, rawlist.Count);
todelete = GetFilesToDelete(jpglist, rawlist);
ListFilesToDelete(todelete);
if (GetConfirmation(todelete.Count))
{
DeleteFiles(todelete);
}
}
private static void ListFilesToDelete(List<FileInfo> todelete)
{
Console.WriteLine("Files to delete:");
foreach (FileInfo item in todelete)
{
Console.WriteLine(item.Name);
}
}
private static void DeleteFiles(List<FileInfo> todelete)
{
int deleteCount = 0;
foreach (FileInfo file in todelete)
{
Console.Write("Deleting {0}", file.Name);
try
{
File.Delete(file.FullName);
Console.WriteLine(" - OK");
deleteCount++;
}
catch(Exception e)
{
Console.WriteLine(" - {0}", e.Message);
}
}
Console.WriteLine("Deleted {0} file{1}.", deleteCount, (deleteCount > 1 ? "s" : ""));
}
private static bool GetConfirmation(int count)
{
Console.WriteLine("Delete {0} files? y/n", count);
return Console.ReadLine().ToLower().Trim() == "y";
}
private static List<FileInfo> GetFilesToDelete(List<FileInfo> jpglist, List<FileInfo> rawlist)
{
List<FileInfo> output = new List<FileInfo>();
FileInfo temp;
foreach (FileInfo rawfile in rawlist)
{
temp = null;
output.Add(rawfile);
foreach (FileInfo jpgfile in jpglist)
{
if (RemoveExtension(jpgfile.Name) == RemoveExtension(rawfile.Name))
{
output.Remove(rawfile);
temp = jpgfile;
break;
}
}
if (temp != null)
{
jpglist.Remove(temp);
}
}
if (output.Count == 0)
{
Console.WriteLine("Nothing do delete.");
Environment.Exit(0);
}
return output;
}
private static List<FileInfo> GetRaws(string rawFolder)
{
string[] rawextensions = new string[]
{"*.arw", "*.sfr", "*.sr2", "*.bay", "*.crw", "*.cr2",
"*.nef", "*.nrw", "*.orf", "*.pef", "*.ptx", "*.raw",
"*.rw2", "*.srw", "*.x3f", "*.dng"};
List<FileInfo> output = new List<FileInfo>();
string usage = "Create directory raws or pass raw folder name as argument.\n"
+"Pass \"\" to use current folder.";
string path = Environment.CurrentDirectory + "\\" + rawFolder + "\\";
if (! Directory.Exists(path))
{
path = Environment.CurrentDirectory;
path = path.Remove(path.LastIndexOf('\\')) + "\\" + rawFolder + "\\";
if (! Directory.Exists(path))
{
Console.WriteLine(usage);
Environment.Exit(1);
}
}
foreach (string ext in rawextensions)
{
output.AddRange(GetFileList(path, ext));
}
return output;
}
private static List<FileInfo> GetJpegs()
{
List<FileInfo> output = new List<FileInfo>();
string path = Environment.CurrentDirectory;
output = GetFileList(path, "*.jpeg");
output.AddRange(GetFileList(path, "*.jpg"));
return output;
}
private static List<FileInfo> GetFileList(string path, string extension)
{
List<FileInfo> output = new List<FileInfo>();
DirectoryInfo dir = new DirectoryInfo(path);
foreach (FileInfo f in dir.GetFiles(extension))
{
output.Add(f);
}
return output;
}
private static string RemoveExtension(string filename)
{
return filename.Remove(filename.LastIndexOf('.'));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment