-2

Using c# .NET 4.6.1 I have a group of strings I want to use to select "valid" files from a given directory. Here's what I mean:

Here is an example of the strings I want to use to get expected filenames from a directory that are stored in an array. I don't care if the values are stored in an array or a list or some other kind of collection. I can put them in whatever kind of collection that would work best for searching for the file names I want.

//"ValidValues" doesn't have to be an array, 
//it can be a list/array/datatable or some other kind of collection
string[] ValidValues = new string[2] {"ab","cd"};

Files in the directory:

file_1_.txt

file_ab_.blah

file_2_.something

file_cd_.blo

etc.

I want to search for the filenames in the directory using the ValidValues collection that contains the strings I want to use as my search criteria. I've gotten filenames out of a directory in the past using this:

string[] ValidFiles = Directory.GetFiles(@"C:\Drop");

But I don't know how to specify a filter that uses values contained in a collection to be searched for in the filenames in a directory. I know that there are lambda expressions that might be able to do this but I'm not quite sure how to use them. After the code I'm looking for is run I would expect to have

file_ab_.blah

and

file_cd_.blo

in ValidFiles.

I've seen some posts where the search criteria is hard coded and separated by "||" or others where the collection of search strings is looped through but I want to make this dynamic by using ValidValues to find filenames in my directory that contain any of the values in ValidValues. I'm getting the strings contained in ValidValues from a database.

Hopefully this makes sense, I have a strong feeling that it can be done I just don't know the exact syntax to accomplish this. I don't care how it's done, using LINQ or whatever else, I just want the most elegant and efficient method possible. If possible I'd like to avoid looping through the collection of strings I get out of the database and instead use the collection of strings I get out of the database as search criteria in a single statement, possibly using something like a lambda expression or something of the like. Thanks in advance.

Community
  • 1
  • 1
  • The problem with this question is you haven't really done enough research, i mean there are many solutions to this, and with all the information its still a little unclear what you are actually wanting. What i suggest is you break your problems down in to small components. 1 get data from database, 2 enumerate files. 3 Filters files, 4... then when you have a problem with 1 aspect of this then ask the question with your code, and a detailed debugged description of whats not working, what you expect and why – TheGeneral Nov 25 '18 at 04:23
  • Sorry, I'll try to be more specific. 1. I have a collection (a list or array or whatever) called "ValidValues" that contains the strings "ab" and "cd". 2. I have a directory with files in it. 3. I want to find all of the filenames in the directory that contain any of the strings in "ValidValues", in this example "ab" or "cd". 4. I want to do this with a single statement if possible without looping through the elements in "ValidValues". I don't have any code yet to do this, that's why I'm asking the question. Hope this clarifies it a bit. –  Nov 25 '18 at 04:30
  • Searching and research is not about finding some snippet of code that does exactly what you want. Rather to find out about the *concepts* you need to *formulate your own solution* – Ňɏssa Pøngjǣrdenlarp Nov 25 '18 at 07:26
  • I'm sorry I didn't communicate what I had done up to this point more clearly. I have modified my OP to show the steps I went through that led me to the point of asking this question.If my OP could be reviewed so that I would not be restricted from asking questions in the future based off of this I would appreciate it. Thanks. –  Nov 25 '18 at 22:28

1 Answers1

1

Considering you already have your filter keywords in a Collection, you could do the following.

var filteredFileList = ValidStrings.SelectMany(filter => Directory.EnumerateFiles(filePath, $"*{filter}*"))
    .ToList();

Where ValidStrings is collection of your filter keywords, filePath is your location to search

Anu Viswan
  • 17,797
  • 2
  • 22
  • 51
  • 1
    this is along the lines of what I'm looking for. So, say my values from the database are stored in a collection called ValidStrings, how would your suggestion look? –  Nov 25 '18 at 04:27
  • 1
    I have updated the code with your specified collection name 'ValidStrings' – Anu Viswan Nov 25 '18 at 04:29
  • 1
    so `$"*{filter}*"` should stay as is in the statement? –  Nov 25 '18 at 04:32
  • 1
    yes it would, "filter" in this context is the parameter name within the Lambda expression. You can rename it to anything, as per your naming conventions, as long as it matches the LHS – Anu Viswan Nov 25 '18 at 04:33
  • 1
    Just got it set up as you outlined in your answer and it works like a charm. Accepted as answer and tried to up vote but my reputation is too low for it to show. I don't understand why I got a down vote for this question, it seems like I was criticized for not having any code but I didn't have any to show, hence the reason for asking this question. Perhaps someone could do me a solid and up vote this question for me to counter act the down vote?? ; ) Thanks @Anu Viswan –  Nov 25 '18 at 05:35