File Sorter

Posted 29/10/2009 4:03:00 a.m. by Daniel in Development

I got sick of sorting out files... So I wrote a quick little C# app that does all the work for me. Reads in filenames setup as ProgramNameS01E10.avi etc

 

using System;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
namespace TorrentSorter
{
class Program
{
   public static string FileInfoRegex = @"(.+)S(\d+)E(\d+)(.+)";
   public static string PathToScan = "";
   public static string OutputDirectory = "";
   static void Main(string[] args)
   {
      // If there aren't any arguments passed in at start, we need to ask the user for the info
      if (args.Length == 0)
      {
         Console.Write("Directory to scan: ");
         PathToScan = Console.ReadLine();
         Console.Write(string.Format("{0}Directory to move too: ", Environment.NewLine));
         OutputDirectory = Console.ReadLine();
      }
      else
      {
         PathToScan = args[0];
         OutputDirectory = args[1];
         // Allow people to pass in a custom regex pattern if run with arguments
         if (args.Length == 3)
            FileInfoRegex = args[2];
      }
      // Start processing the files
      ProcessDirectory(PathToScan, true);
      Console.WriteLine("Done!");
      Console.ReadKey();
   }
   static void ProcessDirectory(string filePath, bool recursive)
   {
      // If recursive is true, then we'll go through all the directories we can find and process them
      if (recursive)
      {
         foreach (var directory in Directory.GetDirectories(filePath))
         {
            ProcessDirectory(directory, recursive);
         }
}
      // Gets a list of files from given directory
      var listOfFiles = Directory.GetFiles(filePath).ToList();
      foreach (var file in listOfFiles)
      {
         var info = new FileInfo(file);
         var reg = new Regex(FileInfoRegex);
         var match = reg.Match(info.Name);
         // If theres a match, and the extention is not .torrent (for people who store the .torrent files in the directory they're using), start sorting
         if (match.Success && info.Extension.ToLowerInvariant() != ".torrent")
         {
            Console.Write(string.Format("{1}Moving file {0}...", info.Name, Environment.NewLine));
            // Get the program name and season number from the Regex matches
            string programName = FixFileName(match.Groups[1].Value);
            string seasonNumber = match.Groups[2].Value;
            // Work out the program and season folder. Append the word season in front of the number
            string programFolder = Path.Combine(OutputDirectory, programName);
            string seasonFolder = Path.Combine(programFolder, string.Format("Season {0}", seasonNumber));
            // Create the program folder if it doesn't already exist
            if (!Directory.Exists(programFolder))
               Directory.CreateDirectory(programFolder);
            // Create the season folder if it doesn't already exist
            if (!Directory.Exists(seasonFolder))
               Directory.CreateDirectory(seasonFolder);
            // Check that the file doesn't already exist, and copy it
            if (!File.Exists(Path.Combine(seasonFolder, info.Name)))
            {
               File.Copy(file, Path.Combine(seasonFolder, info.Name));
               Console.Write("done.");
            }
            else
            {
               Console.Write("already done.");
            }
         }
      }
   }
   /// 
   /// Removes the crap out of file names
   /// 
   /// 
   /// 
   protected static string FixFileName(string fileName)
   {
      fileName = fileName.Replace(".", " ");
      fileName = fileName.Replace("_", " ");
      fileName = fileName.Replace("-", " ");
      fileName = fileName.Replace("+", " ");
      fileName = fileName.Trim();
      return fileName;
   }
}
}

Don't have a c# complier? Download exe:

FileSorter.exe (6.50 kb)


Comment Title
Name
Email Address
Website URL
Comment
- Your comment will be moderated before it shows up.