Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!

Adding up columns of a comma separated file (CSV)

Aug
1,917
68
Here is my method of adding up columns of a comma separated file (CSV)

The .csv file consists of four columns;
Code:
Account,Trans_Date,Interest,Deposit

While the goal is to sum the Interest and Deposit columns, I also do other calculations to create a Total, which I can use from TCC.
Code:
# tfsa.csv = Account,Trans_Date,Interest,Deposit
#
$TFSA = Import-Csv E:\Documents\vfp\tfsa\tfsa.csv
$Total = 0
$TotalInterest = 0
$TotalDeposit = 0

$TheSum = $TFSA | Measure-Object "Interest" -Sum

$TotalInterest = $TheSum.Sum

$TheSum = $TFSA | Measure-Object "Deposit" -Sum

$TotalDeposit = $TheSum.Sum

$Total = $TotalInterest + $TotalDeposit

$TotalInterest = "{0,9:C}" -f $TotalInterest
$TotalDeposit = "{0,9:C}" -f $TotalDeposit
$Total = "{0,9:C}" -f $Total

Write-Output "Total Interest = $TotalInterest"
Write-Output "Total Deposit  = $TotalDeposit"
Write-Output "                 ---------"
Write-Output "Total          = $Total"

$env:Total = $Total

I run the PowerShell Script from TCC;
Code:
pshell tfsa.ps1
Total Interest =    $18.24
Total Deposit  = $1,899.31
                 ---------
Total          = $1,917.55

I now have the total in a TCC environment variable;
Code:
echo %total
$1,917.55

Joe
 
What if I did not want the environment variable Total formatted?

Just move the line where $Total is put into the environment variable Total;
Code:
$Total = $TotalInterest + $TotalDeposit

$env:Total = $Total

This changes the environment variable to;
Code:
echo %total
1917.55

Joe
 
Back
Top