1.

Solve : Count # Times word in used in multiple word docs?

Answer»

How can I count # Times word in used in multiple word docs contained within subfolders?

For example, I want to count the how many times apples is used in word docs contained within subfolders of a main folderI came up with this so far, but it loops and does display the count

find /c "apples"*.doc C:\Documents and Settings\jbloggs\Desktop\FIND

How can I have a figure displayed?I think I may have a solution

@echo off
findstr /m "apples" *doc "C:\Documents and Settings\jbloggs\Desktop\FIND" >
Results.txt
if %errorlevel%==0 (
echo Found! logged files into Results.txt
) else (
echo No MATCHES found
)

If anyone has anything to add I would appreciate itI couldn't even get this to run. There seems to be too many parameters on the findstr command.

Code: [Select]@echo off
findstr /m "apples" *doc "C:\Documents and Settings\jbloggs\Desktop\FIND" >
Results.txt
if %errorlevel%==0 (
echo Found! logged files into Results.txt
) else (
echo No matches found
)

You mentioned Word documents which have a proprietary file organization and cannot be read by BATCH code. For that you will need a Windows script that can create a instance of the Word application and can do recursion for the sub-folders.

This will work for text documents:

Code: [Select]@echo off
for /f "tokens=*" %%i in ('dir /s /b drive:\path\*.doc') do (
find /i /c "apples" %%i >> results.txt
)

Let us know if you need a script specific for Word documents. Instead of a batch file, you may want
to do this inside of word using a script.
Look at this:
http://word.tips.net/Pages/T001833_Generating_a_Count_of_Word_Occurrences.html
Hi Sidewinder

I need a script specific for word

and to find the the specific word in word doc contained in word .docMany NT batch commands and even POWERSHELL have parameters to indicate recursion. VBScript takes a more do-it-yourself approach:

Code: [Select]wdSearch = "apple" 'Word To Search For
topLevel = "c:\documents" 'Change Directory As Required

Set fso = CreateObject("Scripting.FileSystemObject")
Set wdApp = CreateObject("Word.Application")

Set objFolder = fso.GetFolder(topLevel)
Set colFiles = objFolder.Files
For Each objFile in colFiles
Wscript.Echo objFile.Path, CountTheWord(objFile.Path)
Next

ShowSubfolders fso.GetFolder(topLevel)
wdApp.Quit

Sub ShowSubFolders(Folder)
For Each Subfolder in Folder.SubFolders
Set objFolder = fso.GetFolder(Subfolder.Path)
Set colFiles = objFolder.Files
For Each objFile in colFiles
If UCase(fso.GetExtensionName(objFile)) = "DOC" Then
Wscript.Echo objFile.Path, CountTheWord(objFile.Path)
End If
Next
ShowSubFolders Subfolder
Next
End Sub

Function CountTheWord(objDocument)
wdCount = 0
Set wdDocument = wdApp.Documents.Open(objDocument)
Set objSelection = wdApp.Selection
objSelection.WholeStory
wdTotal = wdApp.Selection.Range.Words.Count
For ctr = 1 To wdTotal
With objSelection.Words(ctr)
If Trim(.Text) = wdSearch Then
wdCount = wdCount + 1
End If
End With
Next
wdDocument.Close
CountTheWord = wdCount
End Function

Save the script with a vbs extension an run from the command prompt as cscript //nologo scriptname.vbs > results.txt

Be sure to change the first two lines in the script to match your specs.

Good luck.

Note: Recursing the directory tree will go to the ends of the earth if necessary. Bottom line: this script will not set any speed records



Discussion

No Comment Found