1

I wanted to search for files in c# begin with a string. I followed the code in the internet

string[] dirs = Directory.GetFiles(@"c:\", "c*");

but instead of finding "c", I want to find files contains a string (i mean the file name for example contain.txt and contain.pdf both has "contain") i created. Here is my code

string filetofind;
string[] dirs = Directory.GetFiles(@"c:\", filetofind + "*");

but it just not working, is there anyway else?

  • 1
    What does "not working" mean? What does `filetofind` look like? And you said _"contains"_, so you probably meant `"*" + filetofin + "*"`. Your current version only looks for file that _start_ with `filetofind`. – René Vogt Jul 02 '18 at 11:51
  • 4
    Do you look for file **names** that contain `filetofind` or do you really mean the file _content_? – René Vogt Jul 02 '18 at 11:54
  • I mean the name, i tried filetofind + "*" and there is a file started with that string but it still not found –  Jul 03 '18 at 04:28
  • @NguyễnNgọcAnh - Then you are not correctly matching the characters. Can you post the actual names of the files you are searching among and the text you are searching for? – Enigmativity Jul 03 '18 at 04:37
  • it is the string that user input. In my case testing it is 696969. I created a file 696969.txt but the code doesnt work –  Jul 03 '18 at 04:47

3 Answers3

3

If

I want to find files contains a string i created

means you want to check file's content (not name) You have to load the file, e.g. (assuming stringToFind doesn't have line breaks)

 string[] dirs = Directory
   .EnumerateFiles(@"c:\", "*.txt");              // all txt files (put the right wildcard)
   .Where(file => File                             
      .ReadLines(file)                            // with at least one line
      .Any(line => line.Contains(stringToFind)))  // which contains stringToFind
   .ToArray(); 

Edit: In case you want files' names which contain c, e.g. "mycode.txt", "Constraints.dat" etc. (but not "demo.com" since c is in the file's extension); you can try *c*.* wild card: file name contains c with any extension:

  string[] dirs = Directory
    .GetFiles(@"c:\", $"*{filetofind}*.*");

In case of elaborated condition, when standard wildcard in not enough, just add Where:

 string[] dirs = Directory
   .EnumerateFiles(@"c:\", "*.*")
   .Where(path => Your_Condition(Path.GetFileNameWithoutExtension(path)))
   .ToArray(); 

For instance, let's test file name for small (not capital) letter c

 string[] dirs = Directory
   .EnumerateFiles(@"c:\", "*.*")
   .Where(path => Path.GetFileNameWithoutExtension(path).Contains('c'))
   .ToArray(); 
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
  • Sorry i mean file name –  Jul 03 '18 at 04:25
  • @Nguyễn Ngọc Anh: in case of *file name* you have to change the wild card a bit; if actual condition is complex enough to be beyond wildcard, you have to put `Where` with the required condition. – Dmitry Bychenko Jul 03 '18 at 07:07
  • @DmitryBychenko: I know this is an old thread! But in your very first piece of code, what if I wanted to search string into a pdf file and not in a text file.. Something like that of windows search - `File contents`. I have posted a question here: https://stackoverflow.com/questions/66548502/file-content-search-c-sharp – StackUseR Mar 09 '21 at 14:12
  • @ Ashish Srivastava: in case of *pdf* file, you can't just put `File.ReadLines(file)` and get all lines; yo have to *parse* it. – Dmitry Bychenko Mar 09 '21 at 15:11
3

To find files where the file name contains "foo", use

var files = Directory.EnumerateFiles("C:\\dir", "*foo*", SearchOption.AllDirectories);

To find files where the text content contains "foo" use:

var files = Directory.EnumerateFiles("C:\\dir", "*", SearchOption.AllDirectories)
    .Where(f => File.ReadAllText(f).Contains("foo"));

This should work, but it will read the entire file as text until you stop enumerating the list of files, so you might want to filter the file list search pattern before reading them. You could also write your own method to inspect each file rather than reading the entire thing into memory for every file.

Substitute SearchOption.AllDirectories for SearchOption.TopDirectoryOnly if you only want to search that directory, and not recursively search subdirectories.

George Helyar
  • 4,319
  • 1
  • 22
  • 20
  • what if "foo" is a string. can i use "*" + foo + "*"? –  Jul 03 '18 at 04:27
  • The final string passed to `EnumerateFiles` is a file search pattern, so that would be `"*"+foo+"*"`. When searching inside content, it's using `string.Contains` so you would just do `.Contains(foo)`. – George Helyar Jul 03 '18 at 05:11
-1

if the file you want find starts with "filetofind" then code is correct. But if "filetofind" comes somewhere between the complete file name then your code must change to

string filetofind;
string[] dirs = Directory.GetFiles(@"c:\", "*filetofind*");