1

I am having some trouble searching directories for Files that have certain criteria in their file names. Below is my code and it captures the correct Files most of the time but sometimes skips the files it needs to capture. What is the best way to do this?

Many thanks.

So below I'm trying to capture All Files in a Directory that have the word "FINAL" and the correct Actual_Date. I have a datatable called dtResult2 that has these Actual_Dates stored in them.

foreach (DataRow drow in dtResult2.Rows)
{          
    //check for Null and start searching if not Null
    if(drow["Well_Name"] != DBNull.Value)
    {
        //Now lets start searching the entire ARCHIVE folder/subfolders for a DWG that has
        //this Well_Name and Actual_Date with FINAL in File Name...

        DirectoryInfo myDir = new DirectoryInfo(myCollection3[v]);  
        //Collect the Final, Approved DWGs only...

        var files = myDir.GetFileSystemInfos().Where(f => f.Name.Contains("FINAL") || f.Name.Contains(drow["Well_Name"].ToString()) || f.Name.Contains(drow["Actual_Date"].ToString()));

        //More code not shown due to premise of question..
    }
}
ebb
  • 9,297
  • 18
  • 72
  • 123
DaBears
  • 1,693
  • 3
  • 14
  • 17

2 Answers2

0

there is nothing wrong with the code probably the where condition is wrong.

I can suggest you to convert it into a classic for/foreach which allows you to debug the conditions and is also more performant. You can even convert it back to a LINQ expression as soon as you have identify the bug

Massimiliano Peluso
  • 26,379
  • 6
  • 61
  • 70
0

Most likely the problem lies with the use of the Contains method which is case sensitive:

f.Name.Contains("FINAL") will not match "Final" it will only match "FINAL".

Use IndexOf for better results for case insensitive comparisons.

Community
  • 1
  • 1
Metro Smurf
  • 37,266
  • 20
  • 108
  • 140