I am trying to search for a particular occurrence of a string in some files belonging to a directory. (The search is also performed in the sub directories. Currently, I came up with a solution something like this.
- Get all filenames inside a directory and its sub directories.
- Open files one by one.
- Search for a particular string
- If it contains, store filename in an array.
-
Continue this till the last file.
string[] fileNames = Directory.GetFiles(@"d:\test", "*.txt", SearchOption.AllDirectories); foreach (string sTem in fileNames) { foreach (string line in File.ReadAllLines(sTem)) { if (line.Contains(SearchString)) { MessageBox.Show("Found search string!"); break; } } }
I think there can be other methods/approach efficient and speeder than this? Using a batch file? OK. Another, solution is to use findstr (but how to use it directly with C# program without a batch file ? What is the most efficient (or more efficient than what I did?)
Code examples are much appreciated!
Found out another solution.
Process myproc = new Process();
myproc.StartInfo.FileName = "findstr";
myproc.StartInfo.Arguments = "/m /s /d:\"c:\\REQs\" \"madhuresh\" *.req";
myproc.StartInfo.RedirectStandardOutput = true;
myproc.StartInfo.UseShellExecute = false;
myproc.Start();
string output = myproc.StandardOutput.ReadToEnd();
myproc.WaitForExit();
Is this execution of a process good ? Comments on this too are welcome!
According to the @AbitChev’s method, a sleek (I don’t know if it’s efficient!). Anyways, it goes on like this. This one searches all the directory as well as the subdirectories!
IEnumerable<string> s = from file in Directory.EnumerateFiles("c:\\directorypath", "*.req", SearchOption.AllDirectories)
from str in File.ReadLines(file)
//where str.Contains("Text@tosearched2")
where str.IndexOf(sSearchItem, StringComparison.OrdinalIgnoreCase) >= 0
select file;
foreach (string sa in s)
MessageBox.Show(sa);
(for case-insensitive search. Maybe that could help someone.)
Please comment! Thanks.
How about somthing like this
This has the advantage of loading only what is required into memory, rather than the names of all the files then, the contents of each file.
I note you are using
String.ContainswhichThis would allow us to do a simple charachter wise compare.
I’d start with a little helper function
Then I’d alter the previous algorithm to use the function like this.
this holds only as many
chars as the search string contains in memory and uses rolling buffer across each file. Theoretically the file could contain no new lines and consume your whole disk, or, your search string could contain a new line.As further work I’d convert the per file part of the algorithm into a function and investigate a multi-threaded approach.
So this would be the internal function,
Then you could process the files in parallel like this