Click here to Skip to main content
15,887,596 members

Welcome to the Lounge

   

For discussing anything related to a software developer's life but is not for programming questions. Got a programming question?

The Lounge is rated Safe For Work. If you're about to post something inappropriate for a shared office environment, then don't post it. No ads, no abuse, and no programming questions. Trolling, (political, climate, religious or whatever) will result in your account being removed.

 
GeneralRe: Recursively Searching for "text" in files in windows 11 Pin
BillWoodruff8-Jan-23 2:10
professionalBillWoodruff8-Jan-23 2:10 
GeneralRe: Recursively Searching for "text" in files in windows 11 Pin
jmaida8-Jan-23 13:29
jmaida8-Jan-23 13:29 
GeneralRe: Recursively Searching for "text" in files in windows 11 Pin
PIEBALDconsult8-Jan-23 3:28
mvePIEBALDconsult8-Jan-23 3:28 
GeneralRe: Recursively Searching for "text" in files in windows 11 Pin
Pete O'Hanlon8-Jan-23 4:07
mvePete O'Hanlon8-Jan-23 4:07 
GeneralRe: Recursively Searching for "text" in files in windows 11 Pin
trønderen8-Jan-23 4:39
trønderen8-Jan-23 4:39 
GeneralRe: Recursively Searching for "text" in files in windows 11 Pin
englebart8-Jan-23 5:04
professionalenglebart8-Jan-23 5:04 
GeneralRe: Recursively Searching for "text" in files in windows 11 Pin
jmaida8-Jan-23 13:24
jmaida8-Jan-23 13:24 
GeneralRe: Recursively Searching for "text" in files in windows 11 Pin
raddevus8-Jan-23 6:21
mvaraddevus8-Jan-23 6:21 
If you have Linqpad -- LINQPad - The .NET Programmer's Playground[^] -- (and what self-respecting C# dev doesn't Roll eyes | :rolleyes: ) then I got your back on this:

Here's a great little findInFiles script I wrote a few years ago when i was frustrated because I couldn't search inside of source code (*.cs) to find specific text items I needed.

Keep in mind I wrote this very quickly bec I was needing to search in files for specific text.
It ignores case and finds all matches ( you can add a parameter to handle this).

it will prompt you for a few items:
1. Directory you want to search (searches all subdirs)
2. text you want to search for.
3. file pattern you want to search against *.*, *.cs, *.txt, etc.

that's it. It'll go through them all and give you some results.

Yes, it's just bruteforce but it works and it's relatively fast and you'll see updated results as it finds the text.

C#
void Main()
{
	Console.WriteLine ("Enter the path you want to search.");
	string searchPath = Console.ReadLine();
	Console.WriteLine(string.Format("Searching : {0}", searchPath));
	DirectoryInfo DirInfo = new DirectoryInfo(searchPath);
	Console.Write("Search Term: ");
	string searchTerm = Console.ReadLine().ToUpper();
	Console.WriteLine(searchTerm);
	Console.WriteLine("Enter the file pattern you want search against.");
	string filePattern = Console.ReadLine();
	try
	{
	var files =  DirInfo.EnumerateFiles(filePattern,SearchOption.AllDirectories);
	foreach (var f in files)
	{
        // Console.WriteLine($"Searching {Path.GetFileName(f.Name)}");  // uncomment to see all file names searched
		string [] allLines = File.ReadAllLines(f.FullName);
		int lineCount = 1;
		bool foundInFile = false;
		foreach (string line in allLines)
		{
			if (line.ToUpper().Contains(searchTerm))
			{
				if (!foundInFile)
				{
					// insures it only prints filename once
					Console.WriteLine("searching {0}", f.FullName.ToUpper());
					foundInFile=true;	
				}
				Console.WriteLine(string.Format("FOUND : {0}  {1}",lineCount, line));
			}
			lineCount++;
		}
		if (foundInFile)
		{
			Console.WriteLine("#############################");
			Console.WriteLine();
		}
	}
	}
	finally
	{
	
	}
}


modified 8-Jan-23 12:41pm.

GeneralRe: Recursively Searching for "text" in files in windows 11 Pin
BillWoodruff8-Jan-23 20:12
professionalBillWoodruff8-Jan-23 20:12 
GeneralRe: Recursively Searching for "text" in files in windows 11 Pin
raddevus9-Jan-23 2:42
mvaraddevus9-Jan-23 2:42 
GeneralRe: Recursively Searching for "text" in files in windows 11 Pin
Richard Deeming8-Jan-23 22:27
mveRichard Deeming8-Jan-23 22:27 
GeneralRe: Recursively Searching for "text" in files in windows 11 Pin
raddevus9-Jan-23 2:44
mvaraddevus9-Jan-23 2:44 
GeneralRe: Recursively Searching for "text" in files in windows 11 Pin
Jörgen Andersson8-Jan-23 21:06
professionalJörgen Andersson8-Jan-23 21:06 
GeneralRe: Recursively Searching for "text" in files in windows 11 Pin
Jörgen Andersson8-Jan-23 21:13
professionalJörgen Andersson8-Jan-23 21:13 
GeneralRe: Recursively Searching for "text" in files in windows 11 Pin
GuyThiebaut8-Jan-23 21:31
professionalGuyThiebaut8-Jan-23 21:31 
GeneralRe: Recursively Searching for "text" in files in windows 11 Pin
rjmoses9-Jan-23 3:15
professionalrjmoses9-Jan-23 3:15 
GeneralRe: Recursively Searching for "text" in files in windows 11 Pin
jmaida9-Jan-23 8:41
jmaida9-Jan-23 8:41 
AnswerRe: Use POSIX find and grep. Pin
Jeremy Falcon9-Jan-23 9:31
professionalJeremy Falcon9-Jan-23 9:31 
GeneralRe: Recursively Searching for "text" in files in windows 11 Pin
Stuart Dootson10-Jan-23 5:40
professionalStuart Dootson10-Jan-23 5:40 
GeneralRe: Recursively Searching for "text" in files in windows 11 Pin
jschell10-Jan-23 7:40
jschell10-Jan-23 7:40 
GeneralSorry (Sari) joke of the day Pin
jmaida7-Jan-23 15:16
jmaida7-Jan-23 15:16 
PraiseI've overcome the datasheet, and the hardware from heck. finally Pin
honey the codewitch7-Jan-23 14:43
mvahoney the codewitch7-Jan-23 14:43 
GeneralRe: I've overcome the datasheet, and the hardware from heck. finally Pin
jmaida7-Jan-23 14:57
jmaida7-Jan-23 14:57 
JokeRe: I've overcome the datasheet, and the hardware from heck. finally Pin
englebart8-Jan-23 5:08
professionalenglebart8-Jan-23 5:08 
GeneralRe: I've overcome the datasheet, and the hardware from heck. finally Pin
honey the codewitch8-Jan-23 5:36
mvahoney the codewitch8-Jan-23 5:36 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.