1

I have a bunch of files that look like this where he first 2 characters in the file represent the month (the year can be assumed):

C:\folder\01_File.cvs
C:\folder\02_File.cvs
C:\folder\03_File.cvs

I have the following code in C#:

for (int i = 1;i < 13; i++)
{
    if (File.Exists("C:\folder\" + i.ToString("00") + "_File.cvs"))
    {
        // do something
    }
}

this worked great but now the file name are changing to show "MMDD" in the filename so i am now getting something like this where the first 4 characters represent MMDD

C:\folder\0122_File.cvs
C:\folder\0212_File.cvs
C:\folder\0311_File.cvs

given I don't know (or really care about the day but I can't change the file names, I am trying to figure out the best way to continue to have my code work by either getting the file using some wildcard regex search or any other suggestion.

leora
  • 188,729
  • 360
  • 878
  • 1,366

1 Answers1

0

You can do a pattern search for all the files ending in _File.cvs and then do your logic on each of the files

string[] files = System.IO.Directory.GetFiles(path, "*_File.cvs", 
System.IO.SearchOption.TopDirectoryOnly);
if (files.Length > 0)
{
    //file exist
}
drj
  • 1
  • 1