Link to home
Start Free TrialLog in
Avatar of AlHal2
AlHal2Flag for United Kingdom of Great Britain and Northern Ireland

asked on

File Matching using wildcards

I'm searching a directory

O:\Data_R\Global\Standard_and_Poors\Equities\Constituents_SnP_Citigroup_2.

Files there include the following:
20150304_SPBMI_GL_BMI_CLS.SDC
20150304_SPBMI_GL_MSI_CLS.SDC
20150304_SPBMI_GL_PROP_CLS.SDC

The first part of the name corresponds to the date.

I want to look at files with this pattern:

O:\Data_R\Global\Standard_and_Poors\Equities\Constituents_SnP_Citigroup_2\*_BMI_*.SDC

In other words I only want to look at the first file in the list.

How do I write some C# code to do this?  I'm using visual studio 2008.
Avatar of Kimputer
Kimputer

this should solve your problem (not full code, just something for you to tinker with):

            string abc = "20150304_SPBMI_GL_BMI_CLS.SDC";
            string pat = @".*_BMI_.*\.SDC$";

            Regex r = new Regex(pat, RegexOptions.IgnoreCase);
            
            Match m = r.Match(abc);

         if ( m.Success ){
             MessageBox.Show("yes");

         }
         else {
             MessageBox.Show("no");
         }

Open in new window


also, requires: using System.Text.RegularExpressions;
ASKER CERTIFIED SOLUTION
Avatar of it_saige
it_saige
Flag of United States of America 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 AlHal2

ASKER

This line is in the config file.
O:\Data_R\Global\Standard_and_Poors\Equities\Constituents_SnP_Citigroup_2\*_BMI_*.SDC
With what should I replace it in order to use the regular expression.

There are lots of similar processes using slightly different SDC files.  I want to use the same code for all of them and cater for the for regular expression in the config file.
Using it_saige's solution, it's quite easy:

Read line from config file (O:\Data_R\Global\Standard_and_Poors\Equities\Constituents_SnP_Citigroup_2\*_BMI_*.SDC)

split it up so you get the part after the last slash

use the first part in this line:

DirectoryInfo dir = new DirectoryInfo(@"O:\Data_R\Global\Standard_and_Poors\Equities\Constituents_SnP_Citigroup_2");

use the last part in this line:

var files = dir.EnumerateFiles("*_BMI_*.SDC");

if you use my solution, it's a bit more complicated (depending on your needs and the results of the config file, how many variations are there).
Supply as many results as possible, and I can probably get a function ready.
Avatar of AlHal2

ASKER

thanks.