1

I have a list of filenames:

 List<string> FileList = new List<string>();
 FileList.Add("c:\fn1.rpt");
 FileList.Add("c:\fn2wxy.txt");
 FileList.Add("c:\fn3.pdf");

I have a list of candidates for deletion:

List<string> DeleleList = new List<string>();
DeleteList.Add("fn2")

I have a loop that loops through the file names and I am looking for the correct expression that basically figures out if the file name fragment in the DeleteList matches the current file. In this case, we would delete c:\fn2.txt only. I can iterate the list but it seems as though there must be a Lambda expression somewhere beyond my IQ.

Any help or advice would be greatly appreciated.

0

2 Answers 2

3

You should not search substrings but use System.IO.Path.GetFileNameWithoutExtension.

For example with LINQ (keep only those names which don't appear in the DeleteList):

fileNames = fileNames
  .Where(n => !DeleteList.Contains(System.IO.Path.GetFileNameWithoutExtension(n)))
  .ToList()

If you want to ignore the case, so treat fn2 and FN2 equal, use:

.Where(n => !DeleteList.Contains(System.IO.Path.GetFileNameWithoutExtension(n), StringComparer.OrdinalIgnoreCase))
Sign up to request clarification or add additional context in comments.

2 Comments

In case the OP need it, one can make it case insensitive using .Contains(System.IO.Path.GetFileNameWithoutExtension(n), StringComparer.OrginalIgnoreCase).
Now this is a way bigger thought than I originally had but why the heck not? Seriously -- I wasn't going for the whole directory - just the array of file names. I'll check it out. I am looking for substrings -- kind of like wildcards. Starts with FN2 -- could be FN2i928340983.xyz.
2

Assuming you have the file name in a variable called fileName, then:

  • if you want an exact match, then just use Contains:

if (DeleteList.Contains(fileName))

  • if you want a partial match

if (DeleteList.Any(fileToDelete => fileToDelete.Contains(fileName)))

Instead of Contains you can also use StartsWith or EndsWith.

Edit: I'm assuming your complete code should look like this:

foreach (filename in FileList)
{
   if (DeleteList.Any(fileToDelete => fileToDelete.Contains(fileName)))
   {
       // delete file
   }
}

Though, as others have mentioned, matching on strings like this is not necessarely the best approach. Also, it would probably more intuitive to create a list of files to delete (matching existing files) and iterate through that as a final step and delete the files; like so:

var filesToDelete = FileList.Where(f => DeleteList.Any(df => df.Contains(System.IO.Path.GetFileNameWithoutExtension(f), StringComparer.OrginalIgnoreCase)));
foreach (var filePath in filesToDelete)
{
    //delete file
}

3 Comments

I clarified my question. I'm not able to get your answer to work so maybe with my additional comments, I could do it.
Did you manage to get this working after the update to my answer? & what exactly wasn't working with the original answer?
Sorry it has taken me so long to get back to you. Originally, it wasn't finding the file fragments. I will test this out this weekend. I appreciate your help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.