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 have the option to either grab the DLLs or MSI installer from the downloads section, or you can install the API into your .NET application using NuGet.
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.
The code above enables you to look for particular texts in multiple files and create highlighted HTML output files for each found document.
Printing the Text Search Results
From the result of the search query, you can further extract information about the found documents.
The following will be the output of printing the above search results obtained from the DOCX, PDF, and TXT files:
Documents: 3
Total occurrences: 141
Document: Lorem ipsum.docx
Occurrences: 101
Field: filename
Occurrences: 101
lorem ipsum 1
Field: content
Occurrences: 101
non 94
lorem ipsum 6
Document: Lorem ipsum.pdf
Occurrences: 1
Field: filename
Occurrences: 1
lorem ipsum 1
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:
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 downloads section.
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.