Skip to content

Instantly share code, notes, and snippets.

@Callumpy
Created June 21, 2022 10:43
Show Gist options
  • Save Callumpy/d8d46a407ccde993b581b3edbd7d5679 to your computer and use it in GitHub Desktop.
Save Callumpy/d8d46a407ccde993b581b3edbd7d5679 to your computer and use it in GitHub Desktop.
Get a count of CSV files in a folder structure and count how many rows of data in each CSV
cd $PSScriptRoot
$rootFolder = 'C:\DefaultRoot\'
$files = Get-ChildItem -Path ($rootFolder + "*.csv") -Recurse
$output = foreach ($file in $files) {
try {
[int]$LinesInFile = 0
$reader = New-Object IO.StreamReader ($file.DirectoryName + "\" + $file.Name)
while($reader.ReadLine() -ne $null){ $LinesInFile++ }
[PSCustomObject]@{
'name' = $file.Name;
"directory" = $file.DirectoryName;
"lastWriteTime" = $file.LastWriteTimeUtc;
"lastAccessTime" = $file.LastAccessTimeUtc;
"lines" = $LinesInFile
} | Export-Csv <LOCATION>\files.csv -notype -Append
} catch {}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment