I have searched several categories of forums and cant find this entry - but I know someone had to ask this before...
How do you calculate the sum of several numeric strings in a text file
example lets say the text file has the following numbers on each line
3
3
3
1
keep in mind there are no non-numeric values in the file - just numbers
How do you add these together and write the total (which would be 10) to a new text file.
I know your going to be using the let> / Counter> / WLN> and /Add> commands but I cant piece it together in my head - "even though my head is pretty big"..???
calculating values from a text file
Moderators: JRL, Dorian (MJT support)
-
- Pro Scripter
- Posts: 58
- Joined: Thu Oct 16, 2003 12:53 am
Try something like this:
Readfile>c:\test.txt,file
separate>file,%CRLF%,num
Let>k=1
repeat>k
Let>k=k+1
add>num_1,num_%k%
until>k,num_count
MDL>Total=%num_1%
This assumes each number is on a line of its own, I didn't test to see what would happen if there are blank lines though I think it will pass over them uneventfully.
good luck,
Dick
Readfile>c:\test.txt,file
separate>file,%CRLF%,num
Let>k=1
repeat>k
Let>k=k+1
add>num_1,num_%k%
until>k,num_count
MDL>Total=%num_1%
This assumes each number is on a line of its own, I didn't test to see what would happen if there are blank lines though I think it will pass over them uneventfully.
good luck,
Dick
-
- Pro Scripter
- Posts: 58
- Joined: Thu Oct 16, 2003 12:53 am
Hi Dick,
when I run it I get this error - but its intermittant meanng sometimes I get the error and other times I dont:
---------------------------
Macro Scheduler
---------------------------
Invalid Numeric or Date Format In "Add>num_1,num_%k%"
---------------------------
OK
---------------------------
when I run it I get this error - but its intermittant meanng sometimes I get the error and other times I dont:
---------------------------
Macro Scheduler
---------------------------
Invalid Numeric or Date Format In "Add>num_1,num_%k%"
---------------------------
OK
---------------------------
- Bob Hansen
- Automation Wizard
- Posts: 2475
- Joined: Tue Sep 24, 2002 3:47 am
- Location: Salem, New Hampshire, US
- Contact:
See http://www.mjtnet.com/forum/viewtopic.php?p=8412#8412 for reply to question about this error message.
Hope this was helpful..................good luck,
Bob
A humble man and PROUD of it!
Bob
A humble man and PROUD of it!
B.W.
This is also in response to the thread found here:
http://www.mjtnet.com/forum/viewtopic.php?p=8412#8412
I can duplicate your error. Running the script exactly as posted within MS and as an EXE I can cause the error but it seems to be consistent.
this is what I found.
If C:\test.txt contains one number on each line, the script runs fine.
If C:\test.txt contains lines that are blank, meaning they are completely empty, the script runs fine.
If C:\test.txt contains any non numeric character including spaces at the end of numbers or on otherwise blank lines, the script fails with the Macro Scheduler message:
Let us know,
Dick
This is also in response to the thread found here:
http://www.mjtnet.com/forum/viewtopic.php?p=8412#8412
I can duplicate your error. Running the script exactly as posted within MS and as an EXE I can cause the error but it seems to be consistent.
this is what I found.
If C:\test.txt contains one number on each line, the script runs fine.
If C:\test.txt contains lines that are blank, meaning they are completely empty, the script runs fine.
If C:\test.txt contains any non numeric character including spaces at the end of numbers or on otherwise blank lines, the script fails with the Macro Scheduler message:
If your results are inconsistent, is it possible that the text file(s) are the inconsistency?Invalid Numeric or Date Format In "Add>num_1,num_%k%"
Let us know,
Dick
- Bob Hansen
- Automation Wizard
- Posts: 2475
- Joined: Tue Sep 24, 2002 3:47 am
- Location: Salem, New Hampshire, US
- Contact:
-
- Pro Scripter
- Posts: 58
- Joined: Thu Oct 16, 2003 12:53 am
Hey guys I tried all the suggestion and still could not get it to work so I used VBScript
VBStart
Dim FSO
Dim f
Dim num
Const ForReading = 1
Const ForWriting = 2
Set FSO = CreateObject("Scripting.FileSystemObject")
On Error Resume Next
Set f = FSO.OpenTextFile("FolderTotal.txt", ForReading, False)
Do While f.AtEndOfStream = False
num = num + CInt(f.ReadLine)
Loop
FSO.CreateTextFile "sum.txt", True, False
Set f = FSO.OpenTextFile("Sum.txt", ForWriting, True)
f.WriteLine (num)
Set FSO = Nothing
Set f = Nothing
VBEnd
VBStart
Dim FSO
Dim f
Dim num
Const ForReading = 1
Const ForWriting = 2
Set FSO = CreateObject("Scripting.FileSystemObject")
On Error Resume Next
Set f = FSO.OpenTextFile("FolderTotal.txt", ForReading, False)
Do While f.AtEndOfStream = False
num = num + CInt(f.ReadLine)
Loop
FSO.CreateTextFile "sum.txt", True, False
Set f = FSO.OpenTextFile("Sum.txt", ForWriting, True)
f.WriteLine (num)
Set FSO = Nothing
Set f = Nothing
VBEnd