2

I am using Visual Studio and I am trying to loop through 22,000+ word documents to search for specific words - the code is working but it is very slow, this is what i have so far (any ideas how i can speed this up?):

string docPath = "V:\\Myfolder";
        string writeFile = "N:\\Personal\\DB Development\\SensitivePatients.txt";
        long x = 0;
        string[] words =
        {
                "ige",
                "itu",
                "cop",
                "home",
                "ild",

        };

        foreach (var myfile in Directory.EnumerateFiles(docPath, "*.doc*"))
        {
            Console.WriteLine(x);
            x += 1;
            Microsoft.Office.Interop.Word.Application application = new Microsoft.Office.Interop.Word.Application();
            Document document = application.Documents.Open(myfile, ReadOnly: true);

            // Loop through all words in the document.
            int count = document.Words.Count;
            for (int i = 1; i <= count; i++)
            {
                foreach (string w in words)
                {
                    if (document.Words[i].Text.IndexOf(w, 0, StringComparison.CurrentCultureIgnoreCase) != -1)
                    {
                        using (StreamWriter streamWriter = new StreamWriter(writeFile, true))
                        {
                            streamWriter.WriteLine(myfile);
                            streamWriter.WriteLine(document.Words[i].Text);
                        }
                    }
                };
            }
            // Close word.
            application.Quit();
faulks
  • 21
  • 1
  • 1
    This might help https://stackoverflow.com/questions/21126878/algorithm-to-search-for-a-list-of-words-in-a-text – MarkusAnd Apr 29 '20 at 12:04
  • Is it slow in the searching or the opening of the word documents? Check task manager to see if you are creating multiple instances of Word. – Peter Smith Apr 29 '20 at 12:09

0 Answers0