I want to import a csv file, sum up a column and then export it with sum in the file name. The file looks like this now:
2015-01
Inside the file it looks like this:
Time;E_TotalKwh;E_TodayKwh
01-01-2015;2211.248;4.549
02-01-2015;2212.607;1.359
03-01-2015;2214.048;1.441
04-01-2015;2215.478;1.430
I want to get the sum of the column E_TodayKwh. I'm thinking of using a for each line and add that in a variable. Then I want to rename the file 2015-01;132.678 (for example) where the 132.678 is the sum of the third column and export it.
I'm very new to powershell and having some trouble with summing up the third column.
Import-Csv '*\Jr_2015\2015-01.CSV' -Delimiter ';' | Select-Object E_TodayKwh
I'm trying some things but don't know if I'm on the right track.
CodePudding user response:
This should do it (note that the sum of the third column is 8.779, not 132.678 as you stated in your question). Also, I used _
instead of ;
to separate the original name and the sum, as ;
is not ideal in a filename (though not illegal).
$DataFile = Get-Item '2015-01.csv'
Import-Csv -Path $DataFile -Delimiter ';' |
Measure-Object -Property 'E_TodayKwh' -Sum |
Select-Object @{label="NewName";expression={$DataFile.BaseName '_' $_.Sum $DataFile.Extension}} |
Rename-Item $DataFile