C
C#13mo 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
daysleeper13mo ago
Where()?
Pixel-ink
Pixel-ink13mo ago
What do you mean where? I just explained where. Please read again carefully. Thanks
daysleeper
daysleeper13mo ago
no i meant the extension method Where() use Where instead of FirstOrDefault
Pixel-ink
Pixel-ink13mo ago
Doesn't like Where either.
Jimmacle
Jimmacle13mo ago
read the error?
Pixel-ink
Pixel-ink13mo ago
Yeah, but I don't know how to fix that.
daysleeper
daysleeper13mo ago
occurence is a string
Jimmacle
Jimmacle13mo 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-ink13mo 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
Jimmacle13mo 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-ink13mo ago
That is this line shown in a messagbox.... string[] lines = File.ReadAllLines(file);
Jimmacle
Jimmacle13mo ago
so what part isn't working?
Pixel-ink
Pixel-ink13mo ago
This is the entire contents of the file
Pixel-ink
Pixel-ink13mo 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
x0rld13mo ago
you can join the list for example 🤔
Jimmacle
Jimmacle13mo ago
so read what i said the first time Where does not return a string it returns all strings that match
Pixel-ink
Pixel-ink13mo ago
I actually used Join to display the MessageBox for debugging quickly.
Pixel-ink
Pixel-ink13mo ago
Yeah, I know. I am getting a boolean error. So, what is the fix??
Jimmacle
Jimmacle13mo ago
what boolean error?
Pixel-ink
Pixel-ink13mo ago
Yeah, I read that, not very helpful in my situation.
Jimmacle
Jimmacle13mo ago
it is if you're trying to use Where
Pixel-ink
Pixel-ink13mo ago
See above. I posted a screenshot
Jimmacle
Jimmacle13mo 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
x0rld13mo ago
clearly boolean error kappa
Jimmacle
Jimmacle13mo ago
it's a compiler error
Pixel-ink
Pixel-ink13mo ago
Thats the issue... WHAT am i supposed to be using???
Jimmacle
Jimmacle13mo 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-ink13mo ago
Then why did I even come here. I thought this is where we get help?
Jimmacle
Jimmacle13mo ago
it is we help you learn to program, we don't fix it for you
Pixel-ink
Pixel-ink13mo ago
Well, no disrespect, but you don't sound like helping
Jimmacle
Jimmacle13mo ago
and all you've done is post a screenshot and say "it doesn't work"
Pixel-ink
Pixel-ink13mo 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
Jimmacle13mo ago
i can help once you take my initial advice
Pixel-ink
Pixel-ink13mo ago
Which one?
Jimmacle
Jimmacle13mo 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-ink13mo ago
Yeah, I am looking at that. Here is the current error, but if its all wrong, then it doesn't matter
Jimmacle
Jimmacle13mo 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-ink13mo ago
I am going to try, but I will have to re-write some portions. Its different than what I tried. Thanks
Jimmacle
Jimmacle13mo ago
the hint is that string is not IEnumerable<string>
Pixel-ink
Pixel-ink13mo ago
Yeah, I got that 🙂
Pixel-ink
Pixel-ink13mo 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
Jimmacle13mo ago
you don't need to make a List, you already have a collection that you're calling .Where on
Pixel-ink
Pixel-ink13mo 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
Jimmacle13mo ago
i mean, you still need to use the search condition you actually want to use
Pixel-ink
Pixel-ink13mo 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-ink13mo 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-ink13mo 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-ink13mo 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.
CrumpetMan
CrumpetMan13mo 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-ink13mo ago
A file as dicussed above
CrumpetMan
CrumpetMan13mo ago
What's the query?
Pixel-ink
Pixel-ink13mo 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
CrumpetMan
CrumpetMan13mo ago
var query = txtStr.Where(t => t.Contains("search term"));
var query = txtStr.Where(t => t.Contains("search term"));
Pixel-ink
Pixel-ink13mo 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.
CrumpetMan
CrumpetMan13mo 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-ink13mo 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.
CrumpetMan
CrumpetMan13mo 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-ink13mo ago
This is my first ever c# app.
Pixel-ink
Pixel-ink13mo ago
This is older code. I will post the lastest
CrumpetMan
CrumpetMan13mo 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-ink13mo 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;
}
}
}
CrumpetMan
CrumpetMan13mo 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-------------";
}
}
CrumpetMan
CrumpetMan13mo ago
Pixel-ink
Pixel-ink13mo ago
Yeah it is. Sorry I had a stroke a few years back and have brain damage, so my attention is problematic and slow.
CrumpetMan
CrumpetMan13mo ago
Well there's no need to say that
Pixel-ink
Pixel-ink13mo ago
Well, I am just letting you know why I thought the code you posted was old.
CrumpetMan
CrumpetMan13mo ago
It's alright If this is your first time making a C# app, then I'm impressed
Pixel-ink
Pixel-ink13mo ago
I did basically do that. Except the "CS" string part
CrumpetMan
CrumpetMan13mo ago
Yeah. cs provides syntax highlighting
Pixel-ink
Pixel-ink13mo 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"
CrumpetMan
CrumpetMan13mo ago
It has to do with the foreach loop
Pixel-ink
Pixel-ink13mo 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.
CrumpetMan
CrumpetMan13mo ago
I believe Path.GetFileName is the culprit You're calling it dozens of times I updated this
Pixel-ink
Pixel-ink13mo ago
Got it working. Thanks @coroys
Pixel-ink
Pixel-ink13mo 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-ink13mo 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
Accord13mo 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
More Posts