locked
Count Occurrences of Specific String in Word Documents RRS feed

  • Question

  • I would like to preface this entire post with a clear disclaimer: I have no idea what I'm doing. I've done very minimal PowerShell before, would like to learn more, but have no idea how to go about it. To try and help myself, I'm attempting to help a friend with some paper grading by writing a script that will output the number of periods in a document, whether .docx or .txt. I have it working just fine for plain .txt, but really have no clue whatsoever what I'm doing regarding using Word like this.

    Below is my current progress, where you can see my latest, more-or-less random, very embarrassing attempt to get this working. I would appreciate anyone who's willing to have mercy and at least point me in the right direction.

    $folder = Read-Host -Prompt 'Input your folder (example: C:\Folder) with the files you want graded'
    $txtlist = Get-ChildItem $folder | Where-Object {$_.Name -match '.txt'}
    
    foreach ($txt in $txtlist)
    {
        $txtCount = (Get-Content $folder\$txt | Select-String -Pattern "." -AllMatches).matches.count
        Write-Host "$txt has $txtCount periods."
    }
    
    $doclist = Get-ChildItem $folder | Where-Object {$_.Name -match '.docx'}
    $word = New-Object -ComObject Word.Application
    $Word.Visible = $False
    
    foreach ($doc in $doclist)
    {
        $docCount = ($word.Documents.Open($doc.FullName).Find.Execute('.')).matches.count
        Write-Host "$doc has $docCount periods."
        $word.Application.ActiveDocument.Close()
    }

    • Edited by JSchwin Thursday, September 19, 2019 8:25 PM
    Thursday, September 19, 2019 8:24 PM

All replies

  • A Word document is not text and is in a special structure called XL. You must use the Word object or a Word SDK to gain access.

    If you are not a programmer or trained computer tech this will be a challenge.  I recommend starting by searching for exam-les of how to use PowerShell with Word.  There are many.  Also look in the Gallery for scripts that can search a Word document as that is how you wil need t6o do this.


    \_(ツ)_/

    Thursday, September 19, 2019 9:09 PM
  • Thank you for the insight, jrv!

    I am a mid-level SysAdmin, but purely on the infrastructure side with VMWare and general Windows Server tasks, simple automation, builds, etc. Very minimal PowerShell history here, as I've mentioned -- it just hasn't been needed. But I'm trying to pick it up, and this personal task of mine is, apparently, a good challenge to start with!

    Especially appreciate you clearing up that I'm going to have to work with it as an Object, the way I'm trying to. I'll see if Gallery and the various scripts there have any insights regarding how to do this.

    Friday, September 20, 2019 4:00 PM
  • Hi,
    Was your issue resolved? 
    If you resolved it using our solution, please "mark it as answer" to help other community members find the helpful reply quickly.
    If you resolve it using your own solution, please share your experience and solution here. It will be very beneficial for other community members who have similar questions.
    If no, please reply and tell us the current situation in order to provide further help.
    Best Regards,
    Lee

    Just do it.

    Monday, September 23, 2019 6:26 AM
  • Hi,

    Was your issue resolved?

    If you resolved it using our solution, please "mark it as answer" to help other community members find the helpful reply quickly.

    If you resolve it using your own solution, please share your experience and solution here. It will be very beneficial for other community members who have similar questions.

    If no, please reply and tell us the current situation in order to provide further help.

    Best Regards,

    Lee


    Just do it.

    Thursday, October 3, 2019 2:48 AM