[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Pattern searching for file extensions in c#

Posted on 2013-10-27
9
Medium Priority
?
446 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 59

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
Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

 
LVL 59

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 59

Accepted Solution

by:
Julian Hansen earned 1000 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 59

Expert Comment

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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

656 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