22

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);
        }
rene
  • 41,474
  • 78
  • 114
  • 152
KraggleGrag
  • 223
  • 1
  • 2
  • 6
  • 6
    `Directory.GetFiles(dirPath, "*.wma").Length==0` to filter out every other file except wma extensions. You can look at http://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher(v=vs.110).aspx which will probably serve your purpose – Alex Barac Apr 15 '14 at 10:47

3 Answers3

41
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

musefan
  • 47,875
  • 21
  • 135
  • 185
  • How can I check for more than 2 extensions for example, .xls and .xlsx – Ragnar Aug 04 '16 at 08:06
  • @wearybands: By searching for [the answer](http://stackoverflow.com/questions/3527203/getfiles-with-multiple-extentions) – musefan Aug 04 '16 at 08:29
2

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!

Shamim
  • 434
  • 4
  • 11
1

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
RJFalconer
  • 10,890
  • 5
  • 51
  • 66