powershellexport-to-csvtext-mining

powershell why can't I export this object to csv


I'm trying to count the number of words per PDF file in a source folder and export the name and wordcount to a csv. But my output csv seems to count the number of PDFs (123) although the content of my object seems right.

Snippet

$source = 'C:\Data\SCRIPTS\R\TextMining\PDFs'
$results= @{} 
Get-ChildItem -Path $source -Filter *.pdf -Recurse | ForEach-Object{
    $count = Get-Content $_.FullName | Measure-Object -Word
    $results.Add($_.FullName, $count.Words)}
$results 
Export-Csv C:\Data\SCRIPTS\R\TextMining\PageClustering\PDFs\PGs\PGs_WC.csv -InputObject $results -notypeinformation

I can display the filename and wordcount to the console but the pipe to csv comes out with errors.

Output

IsReadOnly  IsFixedSize IsSynchronized          Keys                                                               Values                                       SyncRoot       Count
FALSE          FALSE        FALSE System.Collections.Hashtable+KeyCollection            System.Collections.Hashtable+ValueCollection    System.Object   123

I'm learning to use PS - what am I doing wrong?


Solution

  • Please try following:

    $source = 'C:\Data\SCRIPTS\R\TextMining\PDFs'
    $results= @()
    Get-ChildItem -Path $source -Filter *.pdf -Recurse | ForEach-Object{
        $count = Get-Content $_.FullName | Measure-Object -Word
        $results += New-Object PSObject -Property @{
                'Name' = $_.FullName
                'Wert' = $count.Words
                }
       }
    $results 
    $results  | Export-Csv C:\Data\SCRIPTS\R\TextMining\PageClustering\PDFs\PGs\PGs_WC.csv  -notype