Octogoon
Octogoon

Reputation: 36

Calculating average of numbers in a text file using VBScript

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.

Upvotes: 0

Views: 572

Answers (1)

Massi FD
Massi FD

Reputation: 398

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

Upvotes: 2

Related Questions