Monday, June 27, 2011

Count the Documents and the Words in the Documents in a Directory

#Clear screen
cls

$intDocs = $intWords = $null
$path = "C:\Users\owner\Documents\Harlequin\Count"
$application = New-Object -ComObject word.application
$application.visible = $false
Get-ChildItem -Path $path -include *.doc,*.docx -Recurse |
ForEach-Object {
"Processing $($_.fullname)"
$document = $application.documents.open($_.fullname)
$intWords += $document.words.count
$intDocs ++
$document.close() | out-null
}
$application.quit()
$application = $null
[gc]::collect()
[gc]::WaitForPendingFinalizers()
"There are $intDocs documents in the $path folder. They contain a total of $intWords words."

No comments:

Post a Comment