When Excel was my endpoint, I could lazy evaluate any sums by concatenating numbers as long expressions in the batch script:
set total==0
for /f "delims=," %%i in ('findstr ...
set total=!total!+%%i
echo !field1!,!field2!,!total!>> csv-file.txt
In VBA, opening the file caused Excel to evaluate the long strings of =0+4+8+12 in the cell as formulas. I didn’t have to worry about adding numbers in the batch script itself.
It’s a different story with perl and R. First, I had to strip the equal signs:
for /f "tokens=*" %%i in (file.txt) do (
for /f "delims== tokens=1-4" %%j in ("!line!") do (
echo %%j%%k%%l%%m>> for-R.txt
)
)
So cmd would strip the equals signs for me while dividing the line into chunks; I could keep my “x+y+z” expressions, and I would be home free. However, instead of evaluating the field as a single number, R read them as a “factor” type:
> setwd("C:/...")
> x <- read.table(for-R.txt, sep=",", header=TRUE)
> x$total[1]
[1] 0+4+6
Levels 0+4+6 ...
What?
> eval(expression(parse(text="0+4+6")))
10
Okay, opportunity knocked: I would use perl:
#!C:/strawberry/bin/perl/bin
use strict;
use warnings;
# ... code ...
open(my $filehdl, "<", "for-R.txt");
while (<$filehdl>)
my @fields = split(',', $_);
my field1 = $fields[0]
# ...
my calc = eval($field1); # 0+4+6
# ...
print $line . "\n";
# ...
Back in the batch script,
@echo off
...
sum-cumul.pl !file! > for-R-2.txt
Rscript %RSCRIPTS%\gen-report.R
autoitmailer.exe arg1 arg2 arg3
...
endlocal
goto :eof