1

I'm a newbee to programming in batch-files on windows, so hopefully someone can help me: I want to use a batch file to sum up all values of column 3 in a csv-file called 'TEST.csv':

01/01/2013;1;1342
01/01/2013;2;1484
01/01/2013;3;1528

After that, I want to use an if-statement: if the total sum of column 3 is unequal to 0, then start up a specific program blabla.exe

Thanks a lot!

1
  • Unless negative values are allowed, if any line has a non 0 value, there is no need to sum the data. Commented Nov 7, 2013 at 17:42

1 Answer 1

2
setlocal enabledelayedexpansion

for /f "tokens=3 delims=;" %%a in (test.csv) do (
    set /a num=!num!+%%a
)
if !num! GTR 0 blabla.exe

To deal with decimals:

setlocal enabledelayedexpansion

for /f "tokens=3 delims=;" %%a in (test.csv) do (
    set num=!num!+%%a
    )
    set num=!num:~1!
call :Eval !num! ret
if %ret% GTR 0 blabla.exe
exit /b

:Eval in out
setlocal
if exist eval.vbs del eval.vbs
>eval.vbs echo wsh.echo eval("%1")
for /f "delims=" %%a in ( 
  'cscript //nologo eval.vbs' 
) do endlocal & set %~2=%%a
del eval.vbs
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks. However, it doesn't work yet. It says 'missing operator' 6 times. Any idea why? I've checked the file and there are in this example 6 values unequal to zero. These 6 values have 3 decimals using '.' as decimal sign. Maybe that's the reason?
When I remove the decimals, it works! So, now I only need a way to deal with these decimals in the csv-file.
Batch and floating point math don't get along to well. I usually farm that out to a little vbscript function. See the above edit.
Thank you Matt. I managed to get generate an automatic test.csv-file with only integers, which makes it less complicated.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.