Bookmark and Share BitCoin Donate: 13t8gAWVpHP2ddxMp88d1NFpZjnFJC6UwK

This is a little Mono c# script I wrote to let us know of new media on our media shares.
I schedule it to run once a day with cron on my linux box.
Initially I wanted to work on file dates, but that didn't work out nicely, so now I 
just cache the current day's files and compare that to the previous days.
Works in windows and linux. 

using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using System.Net;
using System.Net.Mail;
using System.Text;

public class DirectoryComp
{
    static string moviesDirectory = "/mnt/tanya250/Movies/";
    static string seriesDirectory = "/mnt/tanya250/Series/";
    static string moviesDirStructurePath = "/home/ashr/scripts/dirstructure.movies.ashr";
    static string seriesDirStructurePath = "/home/ashr/scripts/dirstructure.series.ashr";
    static string fallbackEmailAddress = "hidden@gmail.com";
    static string fromAddress = "root@hidden.com";
    static string fromName = "El Guapo";
    static string smtpServerAddress = "smtp1.hidden.com";
    static string emailSubject = "New media on Orion";
    static string toEmailAddressFilePath = "/home/ashr/scripts/mediaemails";
    static string replacePathSearchString = "/mnt/tanya250";
    static string replacePathReplaceString = "//orion.hidden.com/media$";
    static DateTime compareDate = DateTime.Now;
    static string emailContents = "";
    static List currentFiles;

    static void Main(string[] args)
    {
        currentFiles = getCurrentFiles(moviesDirStructurePath);
        getNextDirectory(moviesDirectory);
        cacheCurrentFiles(currentFiles, moviesDirStructurePath);

        currentFiles = getCurrentFiles(seriesDirStructurePath);
        getNextDirectory(seriesDirectory);
        cacheCurrentFiles(currentFiles, seriesDirStructurePath);

        if (emailContents != "")
        {
            MailMessage mm = new MailMessage();
            mm.IsBodyHtml = false;
            mm.BodyEncoding = Encoding.ASCII;
            mm.To.Add(BuildTo());
            mm.From = new MailAddress(fromAddress, fromName);
            mm.Subject = emailSubject;
            mm.Body = emailContents;

            SmtpClient client = new SmtpClient(smtpServerAddress);
            client.Send(mm);
        }
    }

    static void getNextDirectory(string RootDirectory)
    {
            foreach (string directory in Directory.GetDirectories(RootDirectory))
            {
                foreach (string file in Directory.GetFiles(directory)) CheckDate(file);
                getNextDirectory(directory);
            }
        }

    static void CheckDate(string Filename)
    {
        FileInfo fi = new FileInfo(Filename);

        if (!currentFiles.Contains(Filename))
        {
            //newFiles.Add(Filename);
            currentFiles.Add(Filename);
            if (emailContents == "")
            {
                emailContents += "The following new file(s) have been found on the media shares:\r\r";
            }
            emailContents += fi.Name + "            ----->              " + remapDir(fi.DirectoryName) + "\r";
        }
        return;
    }

    static string remapDir(string Dir)
    {
        return Dir.Replace(replacePathSearchString, replacePathReplaceString).Replace("/", "\\");
    }

    static string BuildTo()
    {
        try
        {
            StreamReader reader = new StreamReader(toEmailAddressFilePath);
            string emails = reader.ReadToEnd();
            reader.Close();
            return emails;
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
            return fallbackEmailAddress;
        }
    }

    static List getCurrentFiles(string Path)
    {
        List files = new List();

        StreamReader reader = new StreamReader(Path);
        while (!reader.EndOfStream)
        {
            files.Add(reader.ReadLine());
        }
        reader.Close();

        return files;
    }

    static void cacheCurrentFiles(List Files, string Path)
    {
        StreamWriter writer = new StreamWriter(Path);
        foreach (string file in Files)
        {
            writer.WriteLine(file);
        }
        writer.Close();
        return;
    }
}