Created
November 28, 2017 14:54
-
-
Save matt40k/633e76f80cbe45a33fed596b8016ba7f to your computer and use it in GitHub Desktop.
Get the number of rows and columns from a CSV file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$file = "C:\tmp\test.csv" | |
$content = Get-Content -Path $file # -Delimiter "," | |
$noOfColumns = 0 | |
$noOfRows = $content.Count # -1 # Remove header row | |
foreach ($line in $content) | |
{ | |
$occurs = Select-String -InputObject $line -Pattern "," -AllMatches | |
$noOfOccurs = $occurs.Matches.Count + 1 | |
if ($noOfOccurs -gt $noOfColumns) | |
{ | |
$noOfColumns = $noOfOccurs | |
} | |
} | |
Write-Host "No of columns: $noOfColumns" | |
Write-Host "No of rows: $noOfRows" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment