I have thousands of documents within a directory and I need to enter a search term and search through the documents for the search term and return those documents. Can this be done with Aspose?
Yes, you can search in the documents within a directory using Aspose.Words. We suggest you please read the following article.
Find and Replace
In your case, we suggest you following solution.
- Get the files name from the desired directory using .NET IO API.
- Iterate through the documents and load them one by one into Aspose.Words’ DOM.
- Use Range.Replace method to find the desired text. This method returns number of replacements made in the document.
- If this method returns a value greater than 0, add the document’s name in the ArraryList/collection.
Do you have a code sample?
For example, you can use the following code to parse .doc/.docx files in a directory and then search string inside each document.
ArrayList list = new ArrayList();
string[] fileNames = Directory.GetFiles("E:\\Temp\\", "*.doc?", SearchOption.TopDirectoryOnly);
foreach (string fileName in fileNames)
{
Document doc = new Document(fileName);
// find/detect keyword in Word document
int count = doc.Range.Replace("pattern", "pattern");
if (count > 0)
{
// Count this document
list.Add(fileName);
}
}
Hope, this helps in achieving what you are looking for.