Hi Experts, I have file with 7 fields (csv). I need a batch with calculates the sum of all the numbers in 5th column.

Field1,Field2,Field3,Field4,100,Field6,Field7

Field1,Field2,Field3,Field4,200,Field6,Field7

Field1,Field2,Field3,Field4,300,Field6,Field7

Field1,Field2,Field3,Field4,400,Field6,Field7Batch should add all the numbers in Column 5 and write a log file saying "Total Sum is equal to : 1000"

Thanks,

Phani Akella

download gawk(see my sig), then use this one liner c:\> gawk -F"," "{s+=$5}END{print \"Total: \"s}" file

Col.5 should contains integers only. Not tested. @echo off cls setlocal enabledelayedexpansion set sum= for /f "tokens=1-5* delims=," %%1 in (inputfile.csv) do ( set /a sum=!sum!+%%5 ) echo "Total Sum is equal to : !sum!">logfile.log exit /b

I think it would be best to initially set sum=0, so you don't get a

"missing expression" error.When your only tool is a hammer, every problem looks like a nail.

do not use batch for mathematics. Period. Use a tool/programming language that has maths capabilities.

Orangeboy - thank you. Ghostdog - Are you suggesting that Set /A in batch scripting does not have math capabilities even tho' they are limited?

Quote=Set Help

SET /A expression The /A switch specifies that the string to the right of the equal sign is a numerical expression that is evaluated. The expression evaluator is pretty simple and supports the following operations, in decreasing order of precedence: () - grouping ! ~ - - unary operators * / % - arithmetic operators + - - arithmetic operators << >> - logical shift & - bitwise and ^ - bitwise exclusive or | - bitwise or = *= /= %= += -= - assignment &= ^= |= <<= >>= , - expression separator

>>Ghostdog - Are you suggesting that Set /A in batch >>scripting does not have math capabilities even tho' they are >>limited? I mean maths capabilities as a whole. Though cmd.exe has its own set of maths, but yes as you said, its limited, and doesn't handle decimals (and sometimes very big numbers).

On 32-bit windows, at least, a 32-bit signed integer

(-2147483648..2147483647) is perfectly adequate for

most tasks.

>>On 32-bit windows, at least, a 32-bit signed integer

>> (-2147483648..2147483647) is perfectly adequate for

>> most tasks.So are you suggesting OP should continue using batch for maths, even though some time in the future his column 5 may have floating points ?

I'm suggesting that this: do not use batch for mathematics. Period.

is ludicrous, at best.

>> is ludicrous, at best. ludicrous? Batch fails at floating point maths. Now you tell me if its still ludicrous to you or not? Maths is not just about integers. Its a whole lot more.

Now you tell me if its still ludicrous to you or not?Hang on, let me check...

Yep.

EDIT:Just so that I'm not misunderstood,checkin the above means check the flow of

discussion/argument NOT check to see whether

floating-point math is possible in batch.

I am discussing with you (and the rest including OP), the possibility of the numbers being in decimals. although OP doesn't show in his samples, any good programmer/coder who have some form of decent coding experience will want to take calculation of decimals/float into consideration. If not, the batch program will break in future if decimals are encountered. why wait for things to happen when you can avoid it. You do not have to agree with me, since my suggestion/advice is mainly for OP.

Hi Wahine, Thanks for the code.Its working fine for small values like 100.00, 2000.00...But My file contails values like

-1507880.34

139488644.7

-9907766.12

431981.98

319187.51

870956.63

-228846.11

297269.13Thanks,

Phani Akella

@phani333. your data has floats/decimals. Batch is a no go for that. Use native tools like vbscript, or see response number 1.

Ask Your Question

Weekly Poll

Do you trust smart speakers to not spy on you?

Discuss in The Lounge

Poll History