C
C#2y ago
Pixel-ink

❔ [RESOLVED] Find all Occurrences in All Files in Folder Issue

I have this code that works, but it only returns one result per file if string exists in file. And, I know that the "FirstOrDefault" is why I only get 1 returned result from one particular file. This particular file has 22 lines that meet the find criteria> Here is my issue. I have Tried "All, and Any" and get error because they are bool conditions. Here is my line.. This code works but I need to know what else can I use instead of "FirstOrDefault" to return All my results that match my search criteria. string occurrence = lines.FirstOrDefault(l => l.Contains("DateTime.Now")); Any input on this small issue will be appreciated a lot.
string dirScanner = (GlobalVar.dataPath);
if (string.IsNullOrWhiteSpace("DateTime.Now"))
return;

string[] allFiles = Directory.GetFiles(dirScanner, "*.cs");
txtCode.Text = "";

foreach (string file in allFiles)
{
string[] lines = File.ReadAllLines(file);
**//string occurrence = lines.All(l => l.Contains("DateTime.Now")); <<---- ERROR If I USE THIS LINE**
string occurrence = lines.FirstOrDefault(l => l.Contains("DateTime.Now"));
if (occurrence != null)
{
txtCode.Text = txtCode.Text + ">>> " + Path.GetFileName(file) + Environment.NewLine;
txtCode.Text = txtCode.Text + occurrence + Environment.NewLine;
txtCode.Text = txtCode.Text + "---------------------------------------------------------------" + Environment.NewLine;
}
}
string dirScanner = (GlobalVar.dataPath);
if (string.IsNullOrWhiteSpace("DateTime.Now"))
return;

string[] allFiles = Directory.GetFiles(dirScanner, "*.cs");
txtCode.Text = "";

foreach (string file in allFiles)
{
string[] lines = File.ReadAllLines(file);
**//string occurrence = lines.All(l => l.Contains("DateTime.Now")); <<---- ERROR If I USE THIS LINE**
string occurrence = lines.FirstOrDefault(l => l.Contains("DateTime.Now"));
if (occurrence != null)
{
txtCode.Text = txtCode.Text + ">>> " + Path.GetFileName(file) + Environment.NewLine;
txtCode.Text = txtCode.Text + occurrence + Environment.NewLine;
txtCode.Text = txtCode.Text + "---------------------------------------------------------------" + Environment.NewLine;
}
}
78 Replies
daysleeper
daysleeper2y ago
Where()?
Pixel-ink
Pixel-inkOP2y ago
What do you mean where? I just explained where. Please read again carefully. Thanks
daysleeper
daysleeper2y ago
no i meant the extension method Where() use Where instead of FirstOrDefault
Pixel-ink
Pixel-inkOP2y ago
Doesn't like Where either.
Jimmacle
Jimmacle2y ago
read the error?
Pixel-ink
Pixel-inkOP2y ago
Yeah, but I don't know how to fix that.
daysleeper
daysleeper2y ago
occurence is a string
Jimmacle
Jimmacle2y ago
you want a collection of all lines that contain the string it's giving you that you can't assign that to a variable defined as a single string
Pixel-ink
Pixel-inkOP2y ago
Okay... I am getting all the strings in the for loop. Then I guess I am not fetcing the collection properly in this loop. Any advice code wise?
Jimmacle
Jimmacle2y ago
no, somehow you're printing what you intended to write as code as a string or is that actually part of the text thonk
Pixel-ink
Pixel-inkOP2y ago
That is this line shown in a messagbox.... string[] lines = File.ReadAllLines(file);
Jimmacle
Jimmacle2y ago
so what part isn't working?
Pixel-ink
Pixel-inkOP2y ago
This is the entire contents of the file
Pixel-ink
Pixel-inkOP2y ago
As I stated in my original post, its this line... (except I am trying "Where" now) string occurrence = lines.Where(lines.Contains("DateTime.Now")); ... I just want to return every line where my search term matches.
x0rld 👻 🎃
you can join the list for example 🤔
Jimmacle
Jimmacle2y ago
so read what i said the first time Where does not return a string it returns all strings that match
Pixel-ink
Pixel-inkOP2y ago
I actually used Join to display the MessageBox for debugging quickly.
Pixel-ink
Pixel-inkOP2y ago
Yeah, I know. I am getting a boolean error. So, what is the fix??
Jimmacle
Jimmacle2y ago
what boolean error?
Pixel-ink
Pixel-inkOP2y ago
Yeah, I read that, not very helpful in my situation.
Jimmacle
Jimmacle2y ago
it is if you're trying to use Where
Pixel-ink
Pixel-inkOP2y ago
See above. I posted a screenshot
Jimmacle
Jimmacle2y ago
your screenshot doesn't match the most recent snippet of code you shared, which one is current? and i'm talking about the error in your screenshot that's not a "boolean error"
x0rld 👻 🎃
clearly boolean error kappa
Jimmacle
Jimmacle2y ago
it's a compiler error
Pixel-ink
Pixel-inkOP2y ago
Thats the issue... WHAT am i supposed to be using???
Jimmacle
Jimmacle2y ago
i can't tell you that... it's your problem you're trying to solve read the error, it tells you what type Where is returning and the example i linked shows you what you can do with that
Pixel-ink
Pixel-inkOP2y ago
Then why did I even come here. I thought this is where we get help?
Jimmacle
Jimmacle2y ago
it is we help you learn to program, we don't fix it for you
Pixel-ink
Pixel-inkOP2y ago
Well, no disrespect, but you don't sound like helping
Jimmacle
Jimmacle2y ago
and all you've done is post a screenshot and say "it doesn't work"
Pixel-ink
Pixel-inkOP2y ago
Umm,, you didn't see all the code I implemented and tried. Why not help me "LEARN" something new here instead of being so hard-nosed.
Jimmacle
Jimmacle2y ago
i can help once you take my initial advice
Pixel-ink
Pixel-inkOP2y ago
Which one?
Jimmacle
Jimmacle2y ago
i linked you the documentation for Where that shows you what you should be doing with it apply that to fix your error
List<string> fruits =
new List<string> { "apple", "passionfruit", "banana", "mango",
"orange", "blueberry", "grape", "strawberry" };

IEnumerable<string> query = fruits.Where(fruit => fruit.Length < 6);

foreach (string fruit in query)
{
Console.WriteLine(fruit);
}
/*
This code produces the following output:

apple
mango
grape
*/
List<string> fruits =
new List<string> { "apple", "passionfruit", "banana", "mango",
"orange", "blueberry", "grape", "strawberry" };

IEnumerable<string> query = fruits.Where(fruit => fruit.Length < 6);

foreach (string fruit in query)
{
Console.WriteLine(fruit);
}
/*
This code produces the following output:

apple
mango
grape
*/
this is the example on the page
Pixel-ink
Pixel-inkOP2y ago
Yeah, I am looking at that. Here is the current error, but if its all wrong, then it doesn't matter
Jimmacle
Jimmacle2y ago
i know what the current error is, and i'm telling you what you need to know to fix it if you don't try to fix it, idk where to go from there
Pixel-ink
Pixel-inkOP2y ago
I am going to try, but I will have to re-write some portions. Its different than what I tried. Thanks
Jimmacle
Jimmacle2y ago
the hint is that string is not IEnumerable<string>
Pixel-ink
Pixel-inkOP2y ago
Yeah, I got that 🙂
Pixel-ink
Pixel-inkOP2y ago
Okay... updated code. I get an error if I use "lines" as my collection. The error is on image But, If I use join I get no errors... Yeha. But, I get no results at all either.
string dirScanner = (GlobalVar.dataPath);
if (string.IsNullOrWhiteSpace("DateTime.Now"))
return;

string[] allFiles = Directory.GetFiles(dirScanner, "*.cs");
txtCode.Text = "";

foreach (string file in allFiles)
{
string[] lines = File.ReadAllLines(file);

List<string> txtStr = new List<string> {lines) }; << --- ERROR

IEnumerable<string> query = txtStr.Where(txtStr => txtStr.Length > 0);

foreach (string qry in query)
{
txtCode.Text = txtCode.Text + ">>> " + Path.GetFileName(file) + Environment.NewLine;
txtCode.Text = txtCode.Text + qry + Environment.NewLine;
txtCode.Text = txtCode.Text + "------" + Environment.NewLine;
}
}
string dirScanner = (GlobalVar.dataPath);
if (string.IsNullOrWhiteSpace("DateTime.Now"))
return;

string[] allFiles = Directory.GetFiles(dirScanner, "*.cs");
txtCode.Text = "";

foreach (string file in allFiles)
{
string[] lines = File.ReadAllLines(file);

List<string> txtStr = new List<string> {lines) }; << --- ERROR

IEnumerable<string> query = txtStr.Where(txtStr => txtStr.Length > 0);

foreach (string qry in query)
{
txtCode.Text = txtCode.Text + ">>> " + Path.GetFileName(file) + Environment.NewLine;
txtCode.Text = txtCode.Text + qry + Environment.NewLine;
txtCode.Text = txtCode.Text + "------" + Environment.NewLine;
}
}
Jimmacle
Jimmacle2y ago
you don't need to make a List, you already have a collection that you're calling .Where on
Pixel-ink
Pixel-inkOP2y ago
Wasn't sure so, I went by the code. I commented that line where I re-make the list and just edit this line like this... IEnumerable<string> query = lines.Where(lines => lines.Length > 0);
Jimmacle
Jimmacle2y ago
i mean, you still need to use the search condition you actually want to use
Pixel-ink
Pixel-inkOP2y ago
However, when I run it, my app blinks and freezes. I assume its looping way to much or I don't have the code right
Pixel-ink
Pixel-inkOP2y ago
Okay, I know I already have a list, so I guess I don't know what you mean. I am new to C# and I have never tried this before. So, please be patient with this 61 year old man. I had a stroke a few years ago and suffered some brain damage, so I learn slower. I tried this line, but get same error.
Pixel-ink
Pixel-inkOP2y ago
So, my current struggle is. I have a list from a file..... string[] lines = File.ReadAllLines(file); But, I can't seem to figure out how to convert so I can use it with the IEnumarable Where method that was suggested to me. 1) How do I link my "lines" to this List to get rid of error. Basically how do I convert "lines" to work as a Collection String List<string> txtStr = lines; <<--- ERROR 2) Where do I use my search term? I had to discard it because the entire Where line above was broke and didn't work in my last code. DateTime.Now Thanks for any input. .... Did some research... is this correct for converting??? I get no errors.... But, of course does return any results because I don't have my search term in there anymore. Just need to figure that out.
Collection<string> txtStr = new Collection<string>(lines);
Collection<string> txtStr = new Collection<string>(lines);
Pixel-ink
Pixel-inkOP2y ago
Not sure why I get this errore. I says Where needs 2 arguments. I have two. Obviously not the right ones. Any help would be appreciated.
CoRoys
CoRoys2y ago
var txtStr = lines.ToList();
var txtStr = lines.ToList();
What are you trying to query? Not reading the whole thread so please summarise it here
Pixel-ink
Pixel-inkOP2y ago
A file as dicussed above
CoRoys
CoRoys2y ago
What's the query?
Pixel-ink
Pixel-inkOP2y ago
I am searching through every file in a folder with a specific search term. I have the list of all text in each file. But I am stuck on how to filter just the search term and list them the way they are in the file on each line.... and output to a TextBox
CoRoys
CoRoys2y ago
var query = txtStr.Where(t => t.Contains("search term"));
var query = txtStr.Where(t => t.Contains("search term"));
Pixel-ink
Pixel-inkOP2y ago
I did try this but was told it wouldn't work. //string occurrence = lines.Where(l => l.Contains("DateTime.Now")); I will try again with your suggestion.
CoRoys
CoRoys2y ago
What's DateTime.Now doing here? Aren't you supposed to give a file name? If you need a file creation date, you need an instance of System.IO.File Oh alright this is supposed to be a C# code file
Pixel-ink
Pixel-inkOP2y ago
Its just from a file that stores all kinds of code I may use in the future. My app is just a place to store code snippets.
CoRoys
CoRoys2y ago
string dirScanner = (GlobalVar.dataPath);


var allFiles = Directory.GetFiles(dirScanner, "*.cs");
txtCode.Text = "";

foreach (string file in allFiles)
{
var lines = File.ReadAllLines(file).ToList();

var occurrences = lines.Where(l => l.Contains("DateTime.Now"));
foreach(var occurrence in occurences)
{
txtCode.Text = txtCode.Text + ">>> " + Path.GetFileName(file) + Environment.NewLine;
txtCode.Text = txtCode.Text + occurrence + Environment.NewLine;
txtCode.Text = txtCode.Text + "---------------------------------------------------------------" + Environment.NewLine;
}
}
string dirScanner = (GlobalVar.dataPath);


var allFiles = Directory.GetFiles(dirScanner, "*.cs");
txtCode.Text = "";

foreach (string file in allFiles)
{
var lines = File.ReadAllLines(file).ToList();

var occurrences = lines.Where(l => l.Contains("DateTime.Now"));
foreach(var occurrence in occurences)
{
txtCode.Text = txtCode.Text + ">>> " + Path.GetFileName(file) + Environment.NewLine;
txtCode.Text = txtCode.Text + occurrence + Environment.NewLine;
txtCode.Text = txtCode.Text + "---------------------------------------------------------------" + Environment.NewLine;
}
}
if (string.IsNullOrWhiteSpace("DateTime.Now"))
return;
if (string.IsNullOrWhiteSpace("DateTime.Now"))
return;
What is this useless check? Also use proper string interpolation
Pixel-ink
Pixel-inkOP2y ago
This is my first ever c# app.
Pixel-ink
Pixel-inkOP2y ago
This is older code. I will post the lastest
CoRoys
CoRoys2y ago
txtCode.Text = $"{txtCode.Text}>>> {Path.GetFileName(file)}\n{occurrence}\n-------------";
txtCode.Text = $"{txtCode.Text}>>> {Path.GetFileName(file)}\n{occurrence}\n-------------";
Your string should look like this instead. You're setting Text property 3 times and wasting resources It's okay
Pixel-ink
Pixel-inkOP2y ago
Lastest code:
string dirScanner = (GlobalVar.dataPath);
if (string.IsNullOrWhiteSpace("DateTime.Now"))
return;

string[] allFiles = Directory.GetFiles(dirScanner, "*.cs");

txtCode.Text = "";

foreach (string file in allFiles)
{
string[] lines = File.ReadAllLines(file);


Collection<string> txtStr = new Collection<string>(lines);


var query = txtStr.Where(t => t.Contains("DateTime.Now"));
foreach (string qry in query)
{
txtCode.Text = txtCode.Text + ">>> " + Path.GetFileName(file) + Environment.NewLine;
txtCode.Text = txtCode.Text + qry + Environment.NewLine;
txtCode.Text = txtCode.Text + "-----------" + Environment.NewLine;
}
}
}
string dirScanner = (GlobalVar.dataPath);
if (string.IsNullOrWhiteSpace("DateTime.Now"))
return;

string[] allFiles = Directory.GetFiles(dirScanner, "*.cs");

txtCode.Text = "";

foreach (string file in allFiles)
{
string[] lines = File.ReadAllLines(file);


Collection<string> txtStr = new Collection<string>(lines);


var query = txtStr.Where(t => t.Contains("DateTime.Now"));
foreach (string qry in query)
{
txtCode.Text = txtCode.Text + ">>> " + Path.GetFileName(file) + Environment.NewLine;
txtCode.Text = txtCode.Text + qry + Environment.NewLine;
txtCode.Text = txtCode.Text + "-----------" + Environment.NewLine;
}
}
}
CoRoys
CoRoys2y ago
Looks the same to me
string dirScanner = (GlobalVar.dataPath);
var allFiles = Directory.GetFiles(dirScanner, "*.cs");
txtCode.Text = "";

foreach (string file in allFiles)
{
var lines = File.ReadAllLines(file).ToList();

var occurrences = lines.Where(l => l.Contains("DateTime.Now"));
foreach(var occurrence in occurences)
{
txtCode.Text = $"{txtCode.Text}>>> {file}\n{occurrence}\n-------------";
}
}
string dirScanner = (GlobalVar.dataPath);
var allFiles = Directory.GetFiles(dirScanner, "*.cs");
txtCode.Text = "";

foreach (string file in allFiles)
{
var lines = File.ReadAllLines(file).ToList();

var occurrences = lines.Where(l => l.Contains("DateTime.Now"));
foreach(var occurrence in occurences)
{
txtCode.Text = $"{txtCode.Text}>>> {file}\n{occurrence}\n-------------";
}
}
CoRoys
CoRoys2y ago
Pixel-ink
Pixel-inkOP2y ago
Yeah it is. Sorry I had a stroke a few years back and have brain damage, so my attention is problematic and slow.
CoRoys
CoRoys2y ago
Well there's no need to say that
Pixel-ink
Pixel-inkOP2y ago
Well, I am just letting you know why I thought the code you posted was old.
CoRoys
CoRoys2y ago
It's alright If this is your first time making a C# app, then I'm impressed
Pixel-ink
Pixel-inkOP2y ago
I did basically do that. Except the "CS" string part
CoRoys
CoRoys2y ago
Yeah. cs provides syntax highlighting
Pixel-ink
Pixel-inkOP2y ago
Okay, didn't know. Cool. I added CS to the post... looks nice Also, I added your code suggestion. I get no errors, but I get no results either and my App freeze so that I have to "End Tasks"
CoRoys
CoRoys2y ago
It has to do with the foreach loop
Pixel-ink
Pixel-inkOP2y ago
Yeah, figure that. But, all my code was working before I posted for help. But the old code only returedn one line per file that had matching term. I want all lines in each file that matches term searched for.
CoRoys
CoRoys2y ago
I believe Path.GetFileName is the culprit You're calling it dozens of times I updated this
Pixel-ink
Pixel-inkOP2y ago
Got it working. Thanks @coroys
Pixel-ink
Pixel-inkOP2y ago
This is my final results. I also tweaked how the dashed lines appeared. THANKS TO EVERYONE who shared their input with me. I do really appreciate it Here is my final TEST code.....
var boolFile = false;
string dirScanner = (GlobalVar.dataPath);
var allFiles = Directory.GetFiles(dirScanner, "*.cs");

if (string.IsNullOrWhiteSpace("DateTime.Now"))
return;

txtCode.Text = "";

foreach (string file in allFiles)
{
string[] lines = File.ReadAllLines(file);
Collection<string> txtStr = new Collection<string>(lines);
IEnumerable<string> query = txtStr.Where(t => t.Contains("DateTime.Now"));

foreach (string qry in query)
{
if (qry != null)
{
if (boolFile == false)
{
txtCode.Text = txtCode.Text + ">>> " + Path.GetFileName(file) + Environment.NewLine;
boolFile = true;
}
}
txtCode.Text = txtCode.Text + qry + Environment.NewLine;
}

if (boolFile == true)
{
txtCode.Text = txtCode.Text + "---------------------------------------------------------------" + Environment.NewLine;
boolFile = false;
}
}
}
var boolFile = false;
string dirScanner = (GlobalVar.dataPath);
var allFiles = Directory.GetFiles(dirScanner, "*.cs");

if (string.IsNullOrWhiteSpace("DateTime.Now"))
return;

txtCode.Text = "";

foreach (string file in allFiles)
{
string[] lines = File.ReadAllLines(file);
Collection<string> txtStr = new Collection<string>(lines);
IEnumerable<string> query = txtStr.Where(t => t.Contains("DateTime.Now"));

foreach (string qry in query)
{
if (qry != null)
{
if (boolFile == false)
{
txtCode.Text = txtCode.Text + ">>> " + Path.GetFileName(file) + Environment.NewLine;
boolFile = true;
}
}
txtCode.Text = txtCode.Text + qry + Environment.NewLine;
}

if (boolFile == true)
{
txtCode.Text = txtCode.Text + "---------------------------------------------------------------" + Environment.NewLine;
boolFile = false;
}
}
}
Pixel-ink
Pixel-inkOP2y ago
After some refinement and adapting the test into my app. This is now complete. A simple Code Snippets program that uses search of files in a folder. I could of used a database, but I want to try the flat file search instead. This completes my first C# project.
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server