Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maybe I need a Regex?

Tags:

c#

regex

I am making a simple console application for a home project. Basically, it monitors a folder for any files being added.

FileSystemWatcher fsw = new FileSystemWatcher(@"c:\temp");
fsw.Created += new FileSystemEventHandler(fsw_Created);

bool monitor = true;

while (monitor)
{
    fsw.WaitForChanged(WatcherChangeTypes.Created, 1000);
    if(Console.KeyAvailable)
    {
        monitor = false;
    }
}

Show("User has quit the process...", ConsoleColor.Yellow);

When a new files arrives, 'WaitForChanges' gets called, and I can then start the work.

What I need to do is check the filename for patterns. In real life, I am putting video files into this folder. Based on the filename, I will have rules, which move the files into specific directories. So for now, I'll have a list of KeyValue pairs... holding a RegEx (I think?), and a folder. So, if the filename matches a regex, it moves it into the related folder.

An example of a filename is:

CSI- NY.S07E01.The 34th Floor.avi

So, my Regex needs to look at it, and see if the words CSI "AND" (NY "OR" NewYork "OR" New York) exist. If they do, I will then move them to a \Series\CSI\NY\ folder.

I need the AND, because another file example for a different series is:

CSI- Crime Scene Investigation.S11E16.Turn On, Tune In, Drop Dead

So, for this one, I would need to have some NOTs. So, I need to check if the filename has CSI, but NOT ("New York" or "NY" or "NewYork")

Could someone assist me with these RegExs? Or maybe, there's a better method?

like image 819
Craig Avatar asked Dec 18 '25 16:12

Craig


2 Answers

You can try to store conditions in Func<string,bool>

Dictionary<Func<string,bool>,string> dic = new Dictionary<Func<string, bool>, string>();
Func<string, bool> f1 = x => x.Contains("CSI") && ((x.Contains("NY") || x.Contains("New York"));

dic.Add(f1,"C://CSI/");

foreach (var pair in dic)
{
    if(pair.Key.Invoke("CSI- NY.S07E01.The 34th Floor.avi"))
    {
        // copy
        return;
    }
}
like image 183
Stecya Avatar answered Dec 20 '25 06:12

Stecya


I think you have the right idea. The nice thing about this approach is that you can add/remove/edit regular expressions to a config file or some other approach which means you don't have to recompile the project every time you want to keep track of a new show.

A regular expression for CSI AND NY would look something like this. First if you want to check if CSI exists in the filename the regex is simply "CSI". Keep in mind it's case sensitive by default. If you want to check if NY, New York or NewYork exist in the file name the regex is "((NY)|(New York)|(NewYork))" The bars indicate OR and the parenthesis are used to designate groups. In order to combine the two you could run both regexes and in some cases (where perhaps order is unimportant) this might be easier. However if you always expect the show type to come after the syntax would be "(CSI).*((NY)|(New York)|(NewYork))" The period means "any character" and the asterisk means zero or more.

like image 31
Spencer Ruport Avatar answered Dec 20 '25 07:12

Spencer Ruport



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!