Link to home
Start Free TrialLog in
Avatar of rye004
rye004Flag for United States of America

asked on

Pattern searching for file extensions in c#

I have to go through a set of files and see if the extension meets a specific pattern.
Below is the pattern of files that I am currently looking for:

"*.pst *.ost *.e0* *.l0* *.ghs *.gho *.7z *.7z.* *.exp *.ilk *.res *.trg *.tlh *.idb *.pdb *.pch *.opt *.lib *.cab *.mov *.psp *.bmp *.chi *.chm *.com *.dll *.exe *.gif *.png *.hlp *.ivi *.ivt *.ix *.msi *.nls *.obj *.ocx *.rmi *.sys *.tif *.tmp *.ttf *.vbx *.wav *.wpg DRVSPACE.* *.iso *.pdb *.mpeg *.mpg *.ram *.rm *.ai *.aif *.aiff *.bin *.hqx *.snd *.mpe *.wmv *.wma *.xfd *.db *.m4v"

I know Directory.GetFiles has a pattern option.  However it will only let me specify one at a time.  I would like to go through each file individually, then check the extension then do any needed reporting on it.

Any input would be greatly appreciated.
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Here is some code that might get you started. Fill out the rest of the extensions as needed. This assumes your files are in the top folder only - a recursive solution to process subfolders would be needed to do subfolders as well
using System;
using System.IO;
using System.Text.RegularExpressions;

namespace _28278557
{
  class MatchExtensions
  {
    static void Main(string[] args)
    {
      string folderPath = @"G:\foldername";
      string [] wildcard = {"[.+].pst", "[.+].txt", "[.+].xls", "[.+].msg"};

      foreach (string file in Directory.GetFiles(folderPath, "*.*"))
      {
        Console.Write("File: " + file);
        foreach(string wc in wildcard) 
        {
          Match match = Regex.Match(file, wc);
          if (match.Success) 
          {
            Console.WriteLine ("Matched on " + wc);
          }
          else 
          {
            Console.WriteLine("No Match");
          }
        }
      }
      Console.WriteLine ("Done");
    }
  }
}

Open in new window

Avatar of rye004

ASKER

julianH,

Thank you for your posting, it is greatly appreciated.  For the most part I got the code working.  It iterates through the array with no issues.  My only problem is the regular expression does not seem to work.  Would you mind giving me additional input on the regular expression?

Many thanks!
Avatar of rye004

ASKER

I think I just figured it out.  I tried the following and it worked:

{ @"^.*\.dll", @"^.*\.exe", @"^.*\.cab", @"^.*\.htm" };
(apologies for not posting earlier - did not pick up your earlier post)

That is odd - the code I posted worked fine with the regex I used there.


Your regex is using match 0 or more (*) as opposed to 1 or more + and is including the ^ to match from the beginning.

I will look at the code I posted again and post back.
Avatar of rye004

ASKER

Thanks!  Your time is greatly appreciated!
Avatar of rye004

ASKER

curious, did have a sec to look at this?

Many thanks!
ASKER CERTIFIED SOLUTION
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rye004

ASKER

Thank you so much for this.  I understand getting locked up on a project – I have been in the same situation.

So far, this seems to work well.  I will continue playing with it.

Thanks again!
You are welcome - thanks for the points