Search code examples
arraysvbscriptsum

Sum Values In Array Vbscript


In the below code, I was able to loop through 9 (amount of files will vary) txt files and grab a certain line within those text files. The value I grabbed is a quantity value and always listed in the same spot. I'm having some difficulty getting a sum of those quantities.

Current code:

Set oFile2 = FSO.OpenTextFile(File.path, ForReading)

i = 0
'process text files
Do While oFile2.AtEndOfStream <> true
text = oFile2.ReadLine
'WSCript.Echo text
    ReDim Preserve SSheet(i)
SSheet(i) = text
i=i+1
Loop

oFile2.Close
Set oFile2 = Nothing
Set objFSO = Nothing

'find row and strips down quantity
Dim QtyTrim
QtyTrim = Mid((SSheet(30)),6,4)

'convert quantity to int
Dim QtyInt
QtyInt = CInt(QtyTrim)

WSCript.echo QtyInt

Solution

  • Sum Up Numbers From Text Files

    • Calls a procedure (Main) that loops through the files of a given folder (FOLDER_PATH) and for each file with a given extension (FILE_EXTENSION), calls a function (GetQuantity) that retrieves a string (FoundString), defined by the function's constants, converts the string to an integer and passes the integer back to the procedure where it is added to a variable (Total). Finally, shows a message box with the resulting total.
    • Focus on the GetQuantity function and how it is utilized in the calling procedure which may be different than what you had in mind. Not sure why you decided that the preceding code is irrelevant.
    • Keep in mind that the function returns either an integer or Empty.
    Option Explicit
    
    Main
    
    Sub Main()
        
        Const FOLDER_PATH = "C:\Test"
        Const FILE_EXTENSION = "txt"
        
        Dim fso: Set fso = CreateObject("Scripting.FileSystemObject")
        
        If Not fso.FolderExists(FOLDER_PATH) Then
            MsgBox "The path """ & FOLDER_PATH & """ doesn't exist!", vbExclamation
            Exit Sub
        End If
        
        Dim fsoFile, Quantity, Total, FilesFound
        
        For Each fsoFile In fso.GetFolder(FOLDER_PATH).Files
            If StrComp(fso.GetExtensionName(fsoFile), FILE_EXTENSION, _
                    vbTextCompare) = 0 Then
                FilesFound = FilesFound + 1
                Quantity = GetQuantity(fso, fsoFile.Path)
                If Not IsEmpty(Quantity) Then
                    Total = Total + Quantity
                    Quantity = Empty ' reset for the next iteration
                End If
            End If
        Next
        
        If IsEmpty(FilesFound) Then
            MsgBox "No ." & FILE_EXTENSION & "-files found!", vbExclamation
            Exit Sub
        End If
        
        If IsEmpty(Total) Then
            MsgBox "No quantities found!", vbExclamation
            Exit Sub
        End If
    
        ' Continue, e.g.:
        MsgBox "Total: " & Total, vbInformation
        
    End Sub
    
    Function GetQuantity(fso, FilePath)
        
        Const STRING_LINE = 31
        Const STRING_FIRST_CHAR = 6
        Const STRING_LENGTH = 4
        
        Dim fsoTextFile: Set fsoTextFile = fso.OpenTextFile(FilePath, 1)
    
        Dim Line, FoundString
        
        Do Until fsoTextFile.AtEndOfStream
            Line = Line + 1
            If Line = STRING_LINE Then
                FoundString = fsoTextFile.ReadLine
                Exit Do
            Else
                fsoTextFile.ReadLine
            End If
        Loop
        
        fsoTextFile.Close
        
        If IsEmpty(FoundString) Then Exit Function
            
        On Error Resume Next
            GetQuantity = CLng(Mid(FoundString, STRING_FIRST_CHAR, STRING_LENGTH))
        On Error GoTo 0
    
    End Function