Solved

Pattern searching for file extensions in c#

Posted on 2013-10-27
9
387 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 55

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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 55

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 55

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 55

Expert Comment

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

Featured Post

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

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

Suggested Solutions

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…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

789 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