Have you ever wondered how to easily search through lots of files or navigate the challenge of searching within files across different folders? This article has the answers you’re looking for. Let’s dive into the world of text searching and explore how to search for various texts or phrases in different files across diverse folders using C#.

C# API for Scanning Text across Files and Folders

Now, we’ll utilize the GroupDocs.Search for .NET API to explore numerous texts within files of diverse formats across folders. This library empowers us to systematically scan text in an extensive range of file formats, such as Word documents, spreadsheets, presentations, PDF files, Markup files, eBooks, email messages, One Note documents, and ZIP archives. For an in-depth list of supported file formats, refer to the comprehensive documentation.

You can download the API from the releases page, or you can install it into your .NET application using NuGet. The code examples in this article use the API of GroupDocs.Search for .NET 26.10 and the namespaces GroupDocs.Search, GroupDocs.Search.Common, GroupDocs.Search.Highlighters, GroupDocs.Search.Options and GroupDocs.Search.Results.

PM> Install-Package GroupDocs.Search

Searching Multiple Texts in Files across Folders using C#

Follow these steps to perform a text search in multiple files within multiple folders using C# and generate highlighted HTML output files for each found document.

  • Create an Index object with the specified index folder path.
  • Index the parent document folder using the Add method.
  • Define a search query with multiple terms or phrases.
  • Execute the text scanning using the Search method and store the results.
  • Iterate through the search results:
    • Retrieve each found document using the GetFoundDocument method.
    • Retrieve or print any file information for the found document.
    • Set up an OutputAdapter for the desired format and path.
    • Create a Highlighter for the document.
    • Highlight and output the search results to an HTML file using the Highlight method.
string indexFolder = @"c:\MyIndex\"; // Specify path to the index folder
string documentsFolder = @"c:\MyDocuments\"; // Specify path to the parent folder with documents
string outputFolder = @"c:\Output\"; // Specify path to the folder for the HTML files

// Defining a search query with multiple terms and a phrase
string query = "\"lorem ipsum\" OR non OR water";

// Creating an index in the specified folder
Index index = new Index(indexFolder);

// Indexing the parent folder; documents in the subfolders are indexed as well
index.Add(documentsFolder);

// Searching in the index
SearchResult result = index.Search(query);

// Creating the output folder
Directory.CreateDirectory(outputFolder);

for (int i = 0; i < result.DocumentCount; i++)
{
    // Getting the found document
    FoundDocument document = result.GetFoundDocument(i);

    // Setting up the output adapter for an HTML file named after the document
    string outputPath = Path.Combine(outputFolder, Path.GetFileName(document.DocumentInfo.FilePath) + ".html");
    FileOutputAdapter outputAdapter = new FileOutputAdapter(OutputFormat.Html, outputPath);

    // Creating the highlighter and generating the text with the highlighted occurrences
    Highlighter highlighter = new DocumentHighlighter(outputAdapter);
    index.Highlight(document, highlighter);
}

The code above enables you to look for particular texts in multiple files and create highlighted HTML output files for each found document.

Highlighted Text Search Results in HTML format

Printing the Text Search Results

From the result of the search query, you can further extract information about the found documents.

// The 'result' object is taken from the previous example
Console.WriteLine("Documents: " + result.DocumentCount);
Console.WriteLine("Total occurrences: " + result.OccurrenceCount);
for (int i = 0; i < result.DocumentCount; i++)
{
    FoundDocument document = result.GetFoundDocument(i);
    Console.WriteLine("\tDocument: " + Path.GetFileName(document.DocumentInfo.FilePath));
    Console.WriteLine("\tOccurrences: " + document.OccurrenceCount);
    for (int j = 0; j < document.FoundFields.Length; j++)
    {
        FoundDocumentField field = document.FoundFields[j];
        Console.WriteLine("\t\tField: " + field.FieldName);
        Console.WriteLine("\t\tOccurrences: " + field.OccurrenceCount);

        // Printing the found words
        if (field.Terms != null)
        {
            for (int k = 0; k < field.Terms.Length; k++)
            {
                Console.WriteLine("\t\t\t" + field.Terms[k].PadRight(20) + field.TermsOccurrences[k]);
            }
        }

        // Printing the found phrases
        if (field.TermSequences != null)
        {
            for (int k = 0; k < field.TermSequences.Length; k++)
            {
                string phrase = string.Join(" ", field.TermSequences[k]);
                Console.WriteLine("\t\t\t" + phrase.PadRight(20) + field.TermSequencesOccurrences[k]);
            }
        }
    }
}

The following will be the output of printing the above search results obtained from the DOCX, PDF, and TXT files:

Documents: 3
Total occurrences: 200
	Document: Lorem ipsum.docx
	Occurrences: 101
		Field: filename
		Occurrences: 1
			lorem ipsum         1
		Field: content
		Occurrences: 100
			non                 94
			lorem ipsum         6
	Document: Lorem ipsum.pdf
	Occurrences: 60
		Field: filename
		Occurrences: 1
			lorem ipsum         1
		Field: content
		Occurrences: 59
			non                 53
			lorem ipsum         6
	Document: English.txt
	Occurrences: 39
		Field: content
		Occurrences: 39
			water               39

Complete code

Here is the complete C# code that collectively searches the text strings and phrases in multiple files and folders:

string indexFolder = @"c:\MyIndex\"; // Specify path to the index folder
string documentsFolder = @"c:\MyDocuments\"; // Specify path to the parent folder with documents
string outputFolder = @"c:\Output\"; // Specify path to the folder for the HTML files

// Defining a search query with multiple terms and a phrase
string query = "\"lorem ipsum\" OR non OR water";

// Creating an index in the specified folder
Index index = new Index(indexFolder);

// Indexing the parent folder; documents in the subfolders are indexed as well
index.Add(documentsFolder);

// Searching in the index
SearchResult result = index.Search(query);

// Creating the output folder
Directory.CreateDirectory(outputFolder);

for (int i = 0; i < result.DocumentCount; i++)
{
    // Getting the found document
    FoundDocument document = result.GetFoundDocument(i);

    // Setting up the output adapter for an HTML file named after the document
    string outputPath = Path.Combine(outputFolder, Path.GetFileName(document.DocumentInfo.FilePath) + ".html");
    FileOutputAdapter outputAdapter = new FileOutputAdapter(OutputFormat.Html, outputPath);

    // Creating the highlighter and generating the text with the highlighted occurrences
    Highlighter highlighter = new DocumentHighlighter(outputAdapter);
    index.Highlight(document, highlighter);
}

// Printing the search results
Console.WriteLine("Documents: " + result.DocumentCount);
Console.WriteLine("Total occurrences: " + result.OccurrenceCount);
for (int i = 0; i < result.DocumentCount; i++)
{
    FoundDocument document = result.GetFoundDocument(i);
    Console.WriteLine("\tDocument: " + Path.GetFileName(document.DocumentInfo.FilePath));
    Console.WriteLine("\tOccurrences: " + document.OccurrenceCount);
    for (int j = 0; j < document.FoundFields.Length; j++)
    {
        FoundDocumentField field = document.FoundFields[j];
        Console.WriteLine("\t\tField: " + field.FieldName);
        Console.WriteLine("\t\tOccurrences: " + field.OccurrenceCount);

        // Printing the found words
        if (field.Terms != null)
        {
            for (int k = 0; k < field.Terms.Length; k++)
            {
                Console.WriteLine("\t\t\t" + field.Terms[k].PadRight(20) + field.TermsOccurrences[k]);
            }
        }

        // Printing the found phrases
        if (field.TermSequences != null)
        {
            for (int k = 0; k < field.TermSequences.Length; k++)
            {
                string phrase = string.Join(" ", field.TermSequences[k]);
                Console.WriteLine("\t\t\t" + phrase.PadRight(20) + field.TermSequencesOccurrences[k]);
            }
        }
    }
}

Getting a Free License or a Free Trial

Free License

Obtain a temporary license for free to explore this library without constraints.

Free Trial

You can download the free trial from the releases page.

.NET API for Searching within Files and Folders

Conclusion

In this article, we’ve investigated text scanning for searching various texts in numerous files across several folders using C#. Commencing with the search query, we explored within multiple files and folders, highlighting the searched words or phrases in the respective HTML files.

For comprehensive details about the API, readers are advised to refer to the documentation.

Any inquiries or additional discussions can be directed to the available forum.


See Also