0

I am trying to calculate the average between all the numbers in a text file (every number is on a different line), no matter how many numbers there are. (Using VBScript)

Here is an example of the text file that I am trying to find the average between its numbers:

1
9
4
4
2

Also, sorry if I didn't word this well, it's my first question.

Octogoon
  • 36
  • 4

1 Answers1

2

Can you try this script (I suppose that numbers are integer):

filename = "myfile.txt"

Set fso = CreateObject("Scripting.FileSystemObject")
Set f   = fso.OpenTextFile(filename)

i   = 0   'Elements number      
sum = 0   'Sum of values

Do Until f.AtEndOfStream
   sum = sum + CInt(trim(f.ReadLine))
   i = i + 1
Loop

avg = sum / i   'Calculate the average

f.Close

Thank you

Massi FD
  • 360
  • 4
  • 8
  • It just gives me an error at line 14, char 1 that says "Overflow". I have tried what this post said https://stackoverflow.com/questions/38349093/script-error-overflow-cint-vbscript#comment64111185_38349243 but still gives me the same error... – Octogoon Aug 02 '22 at 22:59
  • @Octogoon Change `sum = sum + CInt(trim(f.ReadLine))` to `sum = sum + trim(f.ReadLine)` then it will default to floating point and handle very large numbers. If you still get an error, test first with a very small sample file. – LesFerch Aug 03 '22 at 03:46