Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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))

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.

jart,
I am little lost regarding the following
line.
What is "do" doing in here?
set /p var=<%%a) do (--
Holla.

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

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.

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 definedc:\holla>set /p grandtotal=<runningtotal.txt
c:\holla>set gr
grandtotal=44453c:\holla>
Let me look into your problem holistically now :-)...--
Holla.

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 like1234.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.

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. :-)

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

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.

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.8011:29:09++++C:\holla\bat\t>totalmoney.bat
total = 3988176.1011:29:14++++C:\holla\bat\t>
--
Holla.

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.

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?

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.

![]() |
Batch File 'Set' number t...
|
batch to echo scandinavia...
|

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