Powershell loop through multiple CSV files to get accurate row count

26 views Asked by At

I am having an issue when using Powershell (version 5.1). I am trying to count the number of rows in multiple CSV files. Currently I am using the below - Get-ChildItem "FILENAMEPATTERN*.CSV" | sort |% {$n = $; $c = 0; Get-Content -Path $ -ReadCount 1000 |% { $c += $_.Count }; "$n; $c"}

But this is giving me false (extra) record count than what the actual row count is when I open the file in excel. Our files do tend to have multiline data for fields which might be resulting in an incorrect count.

Although Import-Csv is slower (I think it loads the whole file into memory) it is the only one that returns the accurate number of rows. But it seems like it doesn't allow wildcard search and count rows when looping through the files. What's the most efficient way to get the row count accurately?

I'm looking for something like p:\a\t\h\FILENAMEPATTERN.csv, record_count p:\a\t\h\FILENAMEPATTERN2.csv, record_count p:\a\t\h\FILENAMEPATTERN3.csv, record_count . . .

Not working - $files = Get-ChildItem 'B:\Prod\2023\202302\PNASC.ASSIGNMENTS_CLOSED_FEED_71.*.CSV' | sort

foreach ($file in $files) {Import-Csv($file).count}

0

There are 0 answers