Computing.Net > Forums > Programming > Adding a list of numbers

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Adding a list of numbers

Reply to Message Icon

Name: jart
Date: December 8, 2008 at 19:58:04 Pacific
OS: windows XP
CPU/Ram: 512
Product: Gateway / N8061
Comment:

Hey everyone, I'm trying to extract and sum a list of dollar amounts from a list. I need to be able to get a grand total from this list and pass it to another text file for another application to pick up. The text file that contains the list is pretty simple. It does not have any other data in it except for the list of dollar amounts. Each line has a new dollar amount. There are no lines with multiple values. Each line is a dollar amount containing exactly 2 decimal places and the figures are in the file as floating point numbers. I am trying to use a looping for command to pass each line to a vbs script to add to the new file for the other application to use. With each loop I'm using ">" for the output instead of ">>" so that at the end, only the last output is in the new file so I have just one record, the final grand total of the amounts. Below are both my batch script and my vbs script. I'm terrible with nested commands so I'm sure it's someting in the looping for command rather than the vbs script. I tested the vbs script from the command line by adding two number together mannually and it worked fine with just two numbers. Any help would be MUCH appreciated. I'm sure there are SEVERAL bugs that are obvious to the trained eye but II'm very new at this. I have been searcing the web just kinda learning as I go. :-)

Here is my batch script
@echo off
cscript.exe //H:cscript
setlocal enabledelayedexpansion
echo 0>runningtotal.txt
for /f %%a in (runningtotal.txt) do (
set /p var=<%%a) do (
for /f %%g in (moneyfile.txt) do (
call eval.vbs //nologo %var%+%%g>runningtotal.txt)
)
)
)


set grandtotal=<%runningtotal%
echo Grand Total: %grandtotal%
pause
endlocal


here is my vbs script that it is calling
Set objArgs = WScript.Arguments
wscript.echo eval(objArgs(0))



Sponsored Link
Ads by Google

Response Number 1
Name: jart
Date: December 8, 2008 at 20:12:14 Pacific
Reply:

Well, I allready see one problem after I posted it. The %runningtotal% variable listed near the bottom of the batch script should actually be runngintotal.txt. It still doesn't work after I changed that though.


0

Response Number 2
Name: Holla
Date: December 8, 2008 at 20:40:32 Pacific
Reply:

jart,

try
set /p grandtotal=<runningtotal.txt

--
Holla.


0

Response Number 3
Name: Holla
Date: December 8, 2008 at 20:44:43 Pacific
Reply:

jart,

I am little lost regarding the following
line.
What is "do" doing in here?


set /p var=<%%a) do (

--
Holla.


0

Response Number 4
Name: jart
Date: December 8, 2008 at 20:49:09 Pacific
Reply:

hi holla,

thanks for the reply. i agree that needs to be changed. it still doesn't work even after changing it though. do you see any other problems?

thanks

-jart


0

Response Number 5
Name: jart
Date: December 8, 2008 at 20:53:18 Pacific
Reply:

ah, ok. that line is trying to get the starting value of the runningtotal.txt which i set at 0 to begin with. on each loop, i'm trying to add that value plus the line being processed from the original file (moneyfile.txt) on any given loop. each loop needs to add the next line down to the current runningtotal to get the new running total and keep adding until the final line on the original file is processed. any ideas how i might accomplish this since the method i'm using is obviously wrong.


0

Related Posts

See More



Response Number 6
Name: Holla
Date: December 8, 2008 at 21:07:28 Pacific
Reply:

jart,
well, I just assumed your runningtotal.txt
would contain a number.
It appears that the problem is elsewhere and
your runningtotal.txt does not have anything.
Right?


c:\holla>type runningtotal.txt
44453
c:\holla>set gr
Environment variable gr not defined

c:\holla>set /p grandtotal=<runningtotal.txt

c:\holla>set gr
grandtotal=44453

c:\holla>


Let me look into your problem holistically now :-)...

--
Holla.


0

Response Number 7
Name: Holla
Date: December 8, 2008 at 21:10:43 Pacific
Reply:

Jart,

let me rephrase your question as I understand it. please tell me whether I am right?
You have a file called moneyfile.txt which has lines like

1234.56
93838.32
39373.12
3846356.3
7373.80

you want to add all these numbers and store the resulting number in runningtotal.txt.

Is that right?

--
Holla.


0

Response Number 8
Name: jart
Date: December 8, 2008 at 21:15:04 Pacific
Reply:

yes, that is exactly it. :-) thanks so much for the assistance you are providng.


0

Response Number 9
Name: jart
Date: December 8, 2008 at 21:18:21 Pacific
Reply:

i was able to kinda get it using the set /a command but it wouldn't add decimals. so i tried parsing the the dollars and cents into two sepperate opperations by adding a few more steps but then i ran into problems with leading zeros. mostly due to 08 and 09 having octal values throwing off set /a command's ability to understand the command. at least i think that's what i read was causing it. like i said, i'm still VERY new at all of this. :-)


0

Response Number 10
Name: jart
Date: December 8, 2008 at 21:30:49 Pacific
Reply:

Thanks again for the help. i will check back tomorrow morning to see if there have been any new replys. :-)


0

Response Number 11
Name: Holla
Date: December 8, 2008 at 21:38:30 Pacific
Reply:

jart,
Is it safe to assume that all numbers in the
file have two decimal points?
I mean, in the above example, I have 3846356.3
is that a possible number or will it be 3846356.30
If we can assume that the decimals are always
two digit, it is easy to manipulate.
just remove the decimal with string
operations and then do the arithmetic
operation, and after everything, insert the
decimal :-)

--
Holla.


0

Response Number 12
Name: Holla
Date: December 8, 2008 at 22:00:23 Pacific
Reply:

jart,

If they all have two decimal points,


@echo off
setlocal EnableDelayedExpansion
set totalled=0
for /f %%A in (moneyfile.txt) do (
set readLine=%%A
:StripDecimal
set readLine100=!readLine:.=!
set /a totalled=!totalled! + readLine100
)
set /a Totalby100 = !totalled! / 100
set /a decimalPts = !totalled! %% 100
Echo total = %Totalby100%.%decimalPts%

======================================================
here is how I tested it:
11:29:06++++C:\holla\bat\t>type moneyfile.txt
1234.56
93838.32
39373.12
3846356.30
7373.80

11:29:09++++C:\holla\bat\t>totalmoney.bat
total = 3988176.10

11:29:14++++C:\holla\bat\t>

--
Holla.


0

Response Number 13
Name: Holla
Date: December 8, 2008 at 22:25:55 Pacific
Reply:

jart,

Here is the script that takes care of varying decimal points.
It takes your own approach of using vbs to evaluate expression.

@echo off
setlocal EnableDelayedExpansion
cscript.exe //H:cscript
set totalled=0
for /f %%A in (moneyfile.txt) do (
eval.vbs //Nologo !totalled!+%%A > evaled.tmp
set /p totalled=<evaled.tmp
)
Echo !totalled! > runningtotal.txt

--
Holla.


0

Response Number 14
Name: jart
Date: December 9, 2008 at 08:50:01 Pacific
Reply:

hi holla,

thanks for helping out. i tried the scripts though and neither of them seem to work. i've tried them both on an xp and a windows 2000 pc but had no success. the batch that passes commands to the vbs script does not complete. or at least its going to take so long that it's not practical. i waited several minutes but had no luck. as for the batch script, that's a great idea removing the decimal that way instead of trying to add the dollars and cents sepperately (especially since it would have produced a value higher than one dollar doing it my way so my way had no chance of working). i think that first batch has the best chance of working but it is still not producing anything. i turned the echo on and tested it and found that it was trying to add the variables !totalled! and !readline100! but the line doing it(set /a totalled=!totalled! + readLine100) was missing the exclamation points around the 2nd variable so i thought that would be the only thing to fix but i'm apparently wrong. it still doesn't work. it's producing an error for each loop saying "missing operand". when i look at it echoing the command on each loop, it is actually using the variable names rather than their values in the command. so instead of it reading set /a totalled=0 +12.45 or something like that, it actually reads set /a totalled=!totalled! + !readLine100!. any ideas what might be causing this?


0

Response Number 15
Name: Holla
Date: December 9, 2008 at 21:00:52 Pacific
Reply:

jart,
My machine is a vista.
However, it is important to not have spaces
around some variables.
Please send your scripts to me as
attachments.
my gmail id is hollap.
that is hollap at gmail dot com.

--
Holla.


0

Sponsored Link
Ads by Google
Reply to Message Icon

Batch File 'Set' number t... batch to echo scandinavia...



Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Adding a list of numbers

complete subset of a list www.computing.net/answers/programming/complete-subset-of-a-list/13533.html

Display a list of users on the local machine www.computing.net/answers/programming/display-a-list-of-users-on-the-local-machine/18893.html

Ping list of computers from a txt file www.computing.net/answers/programming/ping-list-of-computers-from-a-txt-file/19843.html