ccmny

divpic

Aug 8th, 2011
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.51 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Drawing;
  6. using System.IO;
  7.  
  8. namespace divpic
  9. {
  10.     class Program
  11.     {
  12.         enum Orientation {LANDSCAPE, PORTRAIT};
  13.  
  14.         static string[] rawextensions = new string[]
  15.             {"*.arw", "*.sfr", "*.sr2", "*.bay", "*.crw", "*.cr2",
  16.              "*.nef", "*.nrw", "*.orf", "*.pef", "*.ptx", "*.raw",
  17.              "*.rw2", "*.srw", "*.x3f", "*.dng"};
  18.  
  19.         static string landFolderName = "\\Poziomy\\";
  20.         static string portFolderName = "\\Piony\\";
  21.         static string rawFolderName = "rawy\\";
  22.  
  23.         static int jpegMoveCount = 0;
  24.         static int rawMoveCount = 0;
  25.  
  26.         static void Main(string[] args)
  27.         {
  28.             List<FileInfo> pictures = GetJpegs();
  29.  
  30.             Console.WriteLine("Found {0} pictures.", pictures.Count);
  31.  
  32.             if (pictures.Count > 0 && GetConfirmation(pictures))
  33.             {
  34.                 SortPictures(pictures);
  35.             }
  36.             Console.WriteLine("Moved {0} jpeg{1} and {2} raw{3}.",
  37.                 jpegMoveCount, jpegMoveCount > 1 ? "s" : "",
  38.                 rawMoveCount, rawMoveCount > 1 ? "s" : "");
  39.             Console.ReadKey();
  40.         }
  41.  
  42.         private static bool GetConfirmation(List<FileInfo> pictures)
  43.         {
  44.             Console.WriteLine("Move {0} pictures? y\\n", pictures.Count);
  45.             return Console.ReadLine().ToLower() == "y";        
  46.         }
  47.  
  48.         private static List<FileInfo> GetJpegs()
  49.         {
  50.             List<FileInfo> output = new List<FileInfo>();
  51.             string path = Environment.CurrentDirectory;
  52.             output = GetFileList(path, "*.jpeg");
  53.             output.AddRange(GetFileList(path, "*.jpg"));
  54.             return output;
  55.         }
  56.  
  57.         private static List<FileInfo> GetFileList(string path, string extension)
  58.         {
  59.             List<FileInfo> output = new List<FileInfo>();
  60.             DirectoryInfo dir = new DirectoryInfo(path);
  61.            
  62.             output.AddRange(dir.GetFiles(extension));
  63.  
  64.             return output;
  65.         }
  66.  
  67.         private static void SortPictures(List<FileInfo> pictures)
  68.         {
  69.             Image photo;
  70.             Orientation orientation = Orientation.LANDSCAPE;
  71.  
  72.             CheckDirectory(Environment.CurrentDirectory, portFolderName);
  73.             CheckDirectory(Environment.CurrentDirectory, landFolderName);
  74.  
  75.             foreach (FileInfo picture in pictures)
  76.             {              
  77.                 photo = Image.FromFile(picture.FullName);
  78.  
  79.                 if (photo.Height <= photo.Width)
  80.                     orientation = Orientation.LANDSCAPE;
  81.                 else
  82.                     orientation = Orientation.PORTRAIT;
  83.  
  84.                 photo.Dispose();
  85.                 MoveFile(picture, orientation);
  86.             }
  87.         }
  88.  
  89.         private static void MoveFile(FileInfo file, Orientation orientation)
  90.         {
  91.             string folderName = orientation == Orientation.LANDSCAPE ? landFolderName : portFolderName;
  92.  
  93.             try
  94.             {
  95.                 File.Move(file.FullName, file.DirectoryName + folderName + file.Name);
  96.                 jpegMoveCount++;
  97.                 TryMovingRaw(file, orientation);
  98.             }
  99.             catch (Exception e)
  100.             {
  101.                 Console.WriteLine("Error moving: {0}\nMessage: {1}", file.FullName, e.Message);
  102.             }
  103.  
  104.  
  105.         }
  106.  
  107.         private static void TryMovingRaw(FileInfo file, Orientation orientantion)
  108.         {
  109.             bool exists = false;
  110.             string rawExtension = "";
  111.  
  112.             foreach (string extension in rawextensions)
  113.             {                
  114.                 if (File.Exists(RemoveExtension(file.FullName) + GetExtension(extension)))
  115.                 {
  116.                     exists = true;
  117.                     rawExtension = GetExtension(extension);
  118.                     break;
  119.                 }
  120.             }
  121.  
  122.             if (exists)
  123.             {
  124.                 MoveRaw(file, rawExtension, orientantion);
  125.             }
  126.         }
  127.  
  128.         private static void MoveRaw(FileInfo file, string rawExtension, Orientation orientantion)
  129.         {
  130.             string folderName =
  131.                 orientantion == Orientation.LANDSCAPE ?
  132.                 landFolderName + rawFolderName :
  133.                 portFolderName + rawFolderName;
  134.  
  135.  
  136.             CheckDirectory(file.DirectoryName, folderName);
  137.             string filename = RemoveExtension(file.FullName) + rawExtension;
  138.             string destination = file.DirectoryName + folderName + RemoveExtension(file.Name) + rawExtension;
  139.  
  140.             try
  141.             {
  142.                 File.Move(filename, destination);
  143.                 rawMoveCount++;
  144.  
  145.             }
  146.             catch(Exception e)
  147.             {
  148.                 Console.WriteLine("Error moving: {0}\nMessage: {1}", filename, e.Message);
  149.             }
  150.         }
  151.  
  152.         private static void CheckDirectory(string directoryName, string dirName)
  153.         {
  154.             if (!Directory.Exists(directoryName + dirName))
  155.             {
  156.                 Directory.CreateDirectory(directoryName + dirName);
  157.                 Console.WriteLine(" Created {0}", directoryName + dirName);
  158.             }
  159.         }
  160.  
  161.         private static string RemoveExtension(string filename)
  162.         {
  163.             return filename.Remove(filename.LastIndexOf('.'));
  164.         }
  165.  
  166.         private static string GetExtension(string filename)
  167.         {
  168.             return filename.Substring(1);
  169.         }
  170.     }
  171. }
Advertisement
Add Comment
Please, Sign In to add comment