I have been trying to read a comma delimited csv file and total a value from each record using the code below that I have put together from various searches:
@echo off
setlocal EnableDelayedExpansion
set "grandtotal=0"
rem Process the file:
call :ProcessFile < %1
exit /B
:ProcessFile
set /P line=
set "total=0"
:nextLine
set line=:EOF
set /P line=
if "!line!" == ":EOF" goto :EOF
set i=0
for %%e in (%line%) do (
set /A i+=1
for %%i in (!i!) do (
if %%i==4 (
echo "value of e - " %%e
set /A htot=+%%e
echo "value of htot - " %htot%
)
)
)
goto nextLine
exit /B
The test csv file contains 4 records with the value in the 4th of 7 fields. I now have a few problems:
The first record is being skipped. No idea as to why. The data in the file should be reading the values of
187.55,109.48,612.82,1028.81for a total of1,938.66.The totalling of the record is not occurring. I am getting a
Missing operator.error on theSet /A htot=+%%eAlthough I am getting that error, the variable
htotdoes seem to get populated, but with the previously read value.
This is the screen output when I run the batch file:
C:\DOS Stuff>try6 testf.csv
"value of e - " 109.48
Missing operator.
"value of htot - "
"value of e - " 612.82
Missing operator.
"value of htot - " 109
"value of e - " 1028.81
Missing operator.
"value of htot - " 612
%htot%. Oh - there is another big problem:set /amath is limited to INT32 (purely integer, no floating point) It can be done in batch, but PowerShell (or any other language) is a much better solution.set /a +=xxx, notset /a =+xxxsetlocal enabledelayedexpansiononly enables delayed expansion. You still have to use it (!htot!instead of%htot%)