Solved

Pattern searching for file extensions in c#

Posted on 2013-10-27
9
375 Views
Last Modified: 2013-11-21
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.
0
Comment
Question by:rye004
  • 5
  • 4
9 Comments
 
LVL 51

Expert Comment

by:Julian Hansen
Comment Utility
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

0
 

Author Comment

by:rye004
Comment Utility
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!
0
 

Author Comment

by:rye004
Comment Utility
I think I just figured it out.  I tried the following and it worked:

{ @"^.*\.dll", @"^.*\.exe", @"^.*\.cab", @"^.*\.htm" };
0
 
LVL 51

Expert Comment

by:Julian Hansen
Comment Utility
(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.
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

Author Comment

by:rye004
Comment Utility
Thanks!  Your time is greatly appreciated!
0
 

Author Comment

by:rye004
Comment Utility
curious, did have a sec to look at this?

Many thanks!
0
 
LVL 51

Accepted Solution

by:
Julian Hansen earned 250 total points
Comment Utility
Apologies for long time replying - was locked up on a project.

Try this code
using System;
using System.IO;
using System.Text.RegularExpressions;

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

			foreach (string file in Directory.GetFiles(folderPath, "*.*"))
			{
				Console.Write("File: " + file);
				matched = false;
				foreach(string wc in wildcard) 
				{
					Match match = Regex.Match(file, wc);
					if (match.Success) 
					{
						matched = true;
						Console.WriteLine (" MATCHED on " + wc);
					}
				}

				if (!matched) 
				{
					Console.WriteLine(" No Match");
				}
			}
			Console.WriteLine ("Done");
		}
	}
}

Open in new window

0
 

Author Closing Comment

by:rye004
Comment Utility
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!
0
 
LVL 51

Expert Comment

by:Julian Hansen
Comment Utility
You are welcome - thanks for the points
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now