Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Viewing all articles
Browse latest Browse all 6937

Get average of column from multiple csv files

$
0
0

Hello, I am seeking help on finding a way to calculate average of one column from multiple csv files and output to a separate csv file based on the name of source csv file.

All of my csv files have the same header structure and they are located in the same folder. I found the following code from google but don't know where to go from there. Measure-object doesn’t seem to do anything. Any help would be much appreciated.

cls
$CSVSourceDir = "C:\Users\iPower\Desktop\testf"
$DataFiles = Get-ChildItem $CSVSourceDir -recurse -force | Where { $_.Name -like "*.csv" } | Foreach-Object -process { $_.FullName }
 [int] $DataFilesCount = $DataFiles.Count
 Write-Output "Discovered $DataFilesCount CSV Data files in $CSVSourceDir"

ForEach ($DataFilesItem in $DataFiles)
     {
         $FileInfo = Get-Item $DataFilesItem
         $LogDate = $FileInfo.LastWriteTime
         Write-Output "Reading data from $DataFilesItem ($LogDate)"
         [array]$CSVData += Import-CSV $DataFilesItem | Measure-Object average -Average
       }
 [int] $CSVDataCount = $CSVData.count


Viewing all articles
Browse latest Browse all 6937

Trending Articles