Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Bat or VBS file to count a specific word in multiple word docs  (Read 12799 times)

0 Members and 1 Guest are viewing this topic.

petreli

    Topic Starter


    Rookie

    Bat or VBS file to count a specific word in multiple word docs
    « on: October 09, 2009, 10:01:12 AM »
    Hi

    I am looking for a code to count the number of times a specific word has been used in multiple word docs, and return the figure in a text file.

    Ie

    Scan through several MS word docs, count the word "apple" and return the figure on a text file.

    Is this possible without having to open each document individually?

    billrich

    • Guest
    Re: Bat or VBS file to count a specific word in multiple word docs
    « Reply #1 on: October 09, 2009, 12:40:28 PM »
    So many bright, helpful people at Computer Hope.
    « Last Edit: October 12, 2009, 11:26:57 AM by billrich »

    petreli

      Topic Starter


      Rookie

      Re: Bat or VBS file to count a specific word in multiple word docs
      « Reply #2 on: October 09, 2009, 01:01:00 PM »
      thanks bill rich

      but not sure how the code should be entered

      can u post it as it should be entered, so i can copy and paste?

      billrich

      • Guest
      Re: Bat or VBS file to count a specific word in multiple word docs
      « Reply #3 on: October 09, 2009, 02:07:15 PM »
      petreli,

      Ask any of the Computer Hope Crew with  2 and 3 thousand posts.

      They are the experts.

      Good luck

      p.s.  Salmon Trout is new but he knows more than anyone. Must be from Canada?
      « Last Edit: October 12, 2009, 11:30:27 AM by billrich »

      billrich

      • Guest
      Re: Bat or VBS file to count a specific word in multiple word docs
      « Reply #4 on: October 09, 2009, 02:10:02 PM »
      I don't know VBS.  Ask Ghost.
      « Last Edit: October 12, 2009, 11:31:55 AM by billrich »

      gh0std0g74



        Apprentice

        Thanked: 37
        Re: Bat or VBS file to count a specific word in multiple word docs
        « Reply #5 on: October 09, 2009, 05:38:31 PM »
        @OP, first of all, what bill uses is grep and wc which are originally *nix tools. there are windows ports of those tools around , so if you did not download them, you can't use it like what bill did.

        secondly, MS words doc are binary files. therefore, you can forget about using that batch bill has written. it doesn't work that way. what you have to do , learn vbscript (or Powershell). See here for example.

        In unix world, I use antiword
        Code: [Select]
        # antiword test.doc| awk '/{for(i=1;i<=NF;i++){if($i~/word/){c++}}}END{print "total count:"c}'
        total count:1

        There is windows port around, see here. I haven't tried it, so i leave it to you to explore
        « Last Edit: October 09, 2009, 07:43:02 PM by gh0std0g74 »

        billrich

        • Guest
        Re: Bat or VBS file to count a specific word in multiple word docs
        « Reply #6 on: October 09, 2009, 06:48:43 PM »
        Read the posts by Ghost.  He has the solution.
        « Last Edit: October 12, 2009, 11:33:00 AM by billrich »

        gh0std0g74



          Apprentice

          Thanked: 37
          Re: Bat or VBS file to count a specific word in multiple word docs
          « Reply #7 on: October 09, 2009, 07:50:10 PM »
          petreli,

          The following suggestion to change a Blaney.doc  to Blaney.txt  might help.
          Be very careful. I would suggest a copy and not a rename( ren ).
          copy Blaney.doc Blaney.txt.

          The Batch find command seems to work ok:


          C:\>find /i /c "City"  Blaney.txt

          ---------- BLANEY.TXT: 2

          C:\>find /i  "City"  Blaney.txt |  wc -l
                 4

          C:\>

          Reference:

          Warning: Be very careful and ask for further advise.

          Use the Windows Search first to find the word you are after and test only one or two Word Document files.  Good Luck
          http://www.pcworld.idg.com.au/forum/290085/search_text_word_dos_documents


          Thu, 05/04/2007 - 18:39
          Re: Search text in Word for DOS documents
          Easy fix. These are actually text files rather than docs in the current sense. If you rename them to .txt rather than .doc you will find that the properties of the files should be unchanged, but surprise surprise XP will now happliy search them. Change a couple to .txt and open in notepad or wordpad to confirm no damage is done. Of course to be really safe, just copy the lot into another directory and change the extensions en masse. To change the lot to .txt all at once, open a command prompt, navigate to the directory containing the files and type ren *.doc *.txt
          Enjoy
          Chris B

          there's no guarantee this method will find the exact count of the words. the usage of find /c means find will count only the lines that contain the word. It would not count accurately when there are several of the same words in one line.

          billrich

          • Guest
          Re: Bat or VBS file to count a specific word in multiple word docs
          « Reply #8 on: October 09, 2009, 08:12:52 PM »
          I was wrong; I'm sorry I took up your time.
          « Last Edit: October 12, 2009, 11:25:30 AM by billrich »

          gh0std0g74



            Apprentice

            Thanked: 37
            Re: Bat or VBS file to count a specific word in multiple word docs
            « Reply #9 on: October 09, 2009, 09:09:54 PM »
            I could not get the VBS code you had a link to. . . to work.
            show why it doesn't work next time. error messages, your code etc....

            a stripped down version
            Code: [Select]
            Set objWord = CreateObject("Word.Application")
            strDocPath = "c:\test\test.doc"
            strSearch = "word"
            Set objDoc = objWord.Documents.Open(strDocPath)
            With objDoc.Content.Find
               .Text = strSearch
               .Format = False
               .Wrap = wdFindStop
               Do While .Execute
                  iStrCount = iStrCount + 1
               Loop
            End With
            If iStrCount = 1 Then
            WScript.Echo strSearch & " appears once in" & vbCrLf & strDocPath
            Else
            WScript.Echo strSearch & " appears " & iStrCount & " times in " & vbCrLf & strDocPath
            End If
            objDoc.Close(wdDoNotSaveChanges)
            objWord.Quit
            Set objWord = Nothing
            Set fso = Nothing

            output
            Code: [Select]
            C:\test>cscript /nologo test.vbs
            word appears 4 times in c:\test\test.doc

            BC_Programmer


              Mastermind
            • Typing is no substitute for thinking.
            • Thanked: 1140
              • Yes
              • Yes
              • BC-Programming.com
            • Certifications: List
            • Computer: Specs
            • Experience: Beginner
            • OS: Windows 11
            Re: Bat or VBS file to count a specific word in multiple word docs
            « Reply #10 on: October 09, 2009, 09:13:42 PM »
            countwords.vbs


            Code: [Select]
            'Word count VBS.

            Dim X,Y,countOf,StrWord,docfile


            Set X = CreateObject("Word.Application")

            docfile="filename.doc"


                For Each y In x.Documents.Add(docfile).Words
                    strword = Trim(Replace(y.Text, vbCrLf, ""))
                    If StrComp(strword, "and", 1) = 0 Then
                        countof = countof + 1
                    
                    End If
                
                
                Next
                x.Quit
                Set x = Nothing
                    
                
                WScript.echo countof

            right now just searches one file. as is could be used with a batch file in some fashion.


            EDIT: darn it, beaten to the punch. Oh well, mine uses a different method. (I think it's slower though)
            I was trying to dereference Null Pointers before it was cool.

            gh0std0g74



              Apprentice

              Thanked: 37
              Re: Bat or VBS file to count a specific word in multiple word docs
              « Reply #11 on: October 09, 2009, 09:29:30 PM »
              right now just searches one file. as is could be used with a batch file in some fashion.

              here's a full version, searches recursively from current directory. No need batch ... :)
              Code: [Select]
              Set objFS=CreateObject("Scripting.FileSystemObject")
              strFolder="c:\test"
              Set objFolder = objFS.GetFolder(strFolder)
              Go (objFolder)
              Sub Go(objDIR)
                If objDIR <> "\System Volume Information" Then
                  For Each eFolder in objDIR.SubFolders
                    Go eFolder
                  Next
              For Each strFile In objDIR.Files
              If objFS.GetExtensionName(strFile) = "doc" Then
              Set x = CreateObject("Word.Application")
              docfile = strFile.Path
              countof = 0
                  For Each y In x.Documents.Add(docfile).Words
                      strword = Trim(Replace(y.Text, vbCrLf, ""))
                      If StrComp(strword, "Word", 1) = 0 Then
                          countof = countof + 1       
                      End If
                  Next
                  x.Quit
                  Set x = Nothing         
                  WScript.echo countof , docfile
              End If
              Next
                End If
              End Sub


              billrich

              • Guest
              Re: Bat or VBS file to count a specific word in multiple word docs
              « Reply #12 on: October 09, 2009, 09:51:31 PM »
              VBS will replace all Batch.  Too bad I'm not bright enough to use it.

              Such Great solutions.  VBS will also replace "C" , "C++"  and all of Unix and shell programming.

              The Computer Hope Crew is on the leading Edge.
              « Last Edit: October 12, 2009, 11:23:03 AM by billrich »

              billrich

              • Guest
              Re: Bat or VBS file to count a specific word in multiple word docs
              « Reply #13 on: October 09, 2009, 10:09:15 PM »
              Great job by the Computer Hope Crew.
              « Last Edit: October 12, 2009, 11:18:53 AM by billrich »

              gh0std0g74



                Apprentice

                Thanked: 37
                Re: Bat or VBS file to count a specific word in multiple word docs
                « Reply #14 on: October 09, 2009, 11:19:25 PM »

                C:\>Where is the OutPut?  I can get No Output from BC or Ghost.

                the error its pretty obvious. you have a corrupt word file. use the attachment i given

                Quote
                How do we know the VBS  code countwords works?
                isn't that trivial. Create a new word document. Type in sample text with words you want to search. Save as a word doc file. Execute the script and if the count is correct, that means it works.

                my output
                Code: [Select]
                C:\test>cscript /nologo test.vbs
                0 C:\test\test\test2.doc
                5 C:\test\test.doc
                0 C:\test\test1.doc



                [Saving space, attachment deleted by admin]