KraggleGrag
KraggleGrag

Reputation: 243

Check if folder contains files with certain extensions

I have other C# code that drops a call recording file into the folder c:\Recordings

Each file has the extension of .wma

I'd like to be able to check the folder every 5 minutes. If the folder contains a file ending in .wma i'd like to execute some code.

If the folder does not contain a file with the .wma extension, i'd like the code to pause for 5 minutes and then re-check (infinitely).

i've started with the following the check if the folder has any files in it at all, but when I run it, it always reports the folder contains files, even though it does not.

string dirPath = @"c:\recordings\";
        if (Directory.GetFiles(dirPath).Length == 0)
        {
            NewRecordingExists = true;
            Console.WriteLine("New Recording exists");
        }
        else
        {
            NewRecordingExists = false;
            Console.WriteLine("No New Recording exists");
            System.Threading.Thread.Sleep(300000);
        }

Upvotes: 24

Views: 48172

Answers (3)

RJFalconer
RJFalconer

Reputation: 11642

Don't use GetFiles if you're going to throw the result away. Use an enumeration so you can exit early:

Directory.EnumerateFiles(Folder, "*.wma", SearchOption.AllDirectories).FirstOrDefault() != null

Upvotes: 1

Shamim
Shamim

Reputation: 444

First of all your logic is reversed! ;)
here is you correct code:

        bool NewRecordingExists;
        string dirPath = @"c:\recordings\";
        string[] fileNames = Directory.GetFiles(dirPath, "*.wma", SearchOption.TopDirectoryOnly);
        if (fileNames.Length != 0)
        {
            NewRecordingExists = true;
            foreach (string fileName in fileNames)
            {
                Console.WriteLine("New Recording exists: {0}", fileName);
                /*  do you process for each file here */
            }
        }
        else
        {
            NewRecordingExists = false;
            Console.WriteLine("No New Recording exists");
            System.Threading.Thread.Sleep(300000);
        }

Although, i recommend using System.Timers.Timer class for you application!

Upvotes: 2

musefan
musefan

Reputation: 48415

if (Directory.GetFiles(dirPath).Length == 0)

This is checking if there are no files... then you are reporting "New Recording exists". I think you just have your logic the wrong way around. else is where it means you have found some files.

In addition, if you want to check for just *.wma files then you can use the GetFiles overload that takes a search pattern parameter, for example:

if (Directory.GetFiles(dirPath, "*.wma").Length == 0)
{
    //NO matching *.wma files
}
else
{
    //has matching *.wma files
}

SIDE NOTE: You may be interested in the FileSystemWatcher, this would enable you to monitor your recordings folder for changes (including when files are added). This would eliminate your requirement to poll every 5 minutes, and you get near-instant execution when the file is added, as opposed to waiting for the 5 minute interval to tick over

Upvotes: 44

Related Questions