Solved

Pattern searching for file extensions in c#

Posted on 2013-10-27
9
398 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
9 Comments
 
LVL 57

Expert Comment

by:Julian Hansen
ID: 39604904
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
ID: 39612713
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
ID: 39612785
I think I just figured it out.  I tried the following and it worked:

{ @"^.*\.dll", @"^.*\.exe", @"^.*\.cab", @"^.*\.htm" };
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 57

Expert Comment

by:Julian Hansen
ID: 39613363
(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
 

Author Comment

by:rye004
ID: 39613369
Thanks!  Your time is greatly appreciated!
0
 

Author Comment

by:rye004
ID: 39636629
curious, did have a sec to look at this?

Many thanks!
0
 
LVL 57

Accepted Solution

by:
Julian Hansen earned 250 total points
ID: 39640964
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
ID: 39666414
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 57

Expert Comment

by:Julian Hansen
ID: 39668155
You are welcome - thanks for the points
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

739 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