Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Drawing;
- using System.IO;
- namespace divpic
- {
- class Program
- {
- enum Orientation {LANDSCAPE, PORTRAIT};
- static string[] rawextensions = new string[]
- {"*.arw", "*.sfr", "*.sr2", "*.bay", "*.crw", "*.cr2",
- "*.nef", "*.nrw", "*.orf", "*.pef", "*.ptx", "*.raw",
- "*.rw2", "*.srw", "*.x3f", "*.dng"};
- static string landFolderName = "\\Poziomy\\";
- static string portFolderName = "\\Piony\\";
- static string rawFolderName = "rawy\\";
- static int jpegMoveCount = 0;
- static int rawMoveCount = 0;
- static void Main(string[] args)
- {
- List<FileInfo> pictures = GetJpegs();
- Console.WriteLine("Found {0} pictures.", pictures.Count);
- if (pictures.Count > 0 && GetConfirmation(pictures))
- {
- SortPictures(pictures);
- }
- Console.WriteLine("Moved {0} jpeg{1} and {2} raw{3}.",
- jpegMoveCount, jpegMoveCount > 1 ? "s" : "",
- rawMoveCount, rawMoveCount > 1 ? "s" : "");
- Console.ReadKey();
- }
- private static bool GetConfirmation(List<FileInfo> pictures)
- {
- Console.WriteLine("Move {0} pictures? y\\n", pictures.Count);
- return Console.ReadLine().ToLower() == "y";
- }
- 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);
- output.AddRange(dir.GetFiles(extension));
- return output;
- }
- private static void SortPictures(List<FileInfo> pictures)
- {
- Image photo;
- Orientation orientation = Orientation.LANDSCAPE;
- CheckDirectory(Environment.CurrentDirectory, portFolderName);
- CheckDirectory(Environment.CurrentDirectory, landFolderName);
- foreach (FileInfo picture in pictures)
- {
- photo = Image.FromFile(picture.FullName);
- if (photo.Height <= photo.Width)
- orientation = Orientation.LANDSCAPE;
- else
- orientation = Orientation.PORTRAIT;
- photo.Dispose();
- MoveFile(picture, orientation);
- }
- }
- private static void MoveFile(FileInfo file, Orientation orientation)
- {
- string folderName = orientation == Orientation.LANDSCAPE ? landFolderName : portFolderName;
- try
- {
- File.Move(file.FullName, file.DirectoryName + folderName + file.Name);
- jpegMoveCount++;
- TryMovingRaw(file, orientation);
- }
- catch (Exception e)
- {
- Console.WriteLine("Error moving: {0}\nMessage: {1}", file.FullName, e.Message);
- }
- }
- private static void TryMovingRaw(FileInfo file, Orientation orientantion)
- {
- bool exists = false;
- string rawExtension = "";
- foreach (string extension in rawextensions)
- {
- if (File.Exists(RemoveExtension(file.FullName) + GetExtension(extension)))
- {
- exists = true;
- rawExtension = GetExtension(extension);
- break;
- }
- }
- if (exists)
- {
- MoveRaw(file, rawExtension, orientantion);
- }
- }
- private static void MoveRaw(FileInfo file, string rawExtension, Orientation orientantion)
- {
- string folderName =
- orientantion == Orientation.LANDSCAPE ?
- landFolderName + rawFolderName :
- portFolderName + rawFolderName;
- CheckDirectory(file.DirectoryName, folderName);
- string filename = RemoveExtension(file.FullName) + rawExtension;
- string destination = file.DirectoryName + folderName + RemoveExtension(file.Name) + rawExtension;
- try
- {
- File.Move(filename, destination);
- rawMoveCount++;
- }
- catch(Exception e)
- {
- Console.WriteLine("Error moving: {0}\nMessage: {1}", filename, e.Message);
- }
- }
- private static void CheckDirectory(string directoryName, string dirName)
- {
- if (!Directory.Exists(directoryName + dirName))
- {
- Directory.CreateDirectory(directoryName + dirName);
- Console.WriteLine(" Created {0}", directoryName + dirName);
- }
- }
- private static string RemoveExtension(string filename)
- {
- return filename.Remove(filename.LastIndexOf('.'));
- }
- private static string GetExtension(string filename)
- {
- return filename.Substring(1);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment