FB sum vbs | 備忘録 (。・_・。)ノ

Option Explicit

Const ForReading = 1
Const ForWriting = 2
Dim fso
Dim dic
Dim file
Dim line
Dim i
Dim csv
Dim key
Dim item1
Dim item2
Dim keys
Dim items
Dim ans
Dim msg
dim fileName
dim filePath
dim filefull

filePath = "C:\tmp\"
fileName = "fukui"
filefull = filePath & fileName & ".csv"

'オブジェクト作成
Set fso = CreateObject("Scripting.FileSystemObject")
Set dic = CreateObject("Scripting.Dictionary")
Set file = fso.OpenTextFile(filefull, ForReading)

line = Split(file.ReadAll, vbCrLf)
file.Close
For i = 1 To UBound(line)
    csv = Split(line(i), ",")
    If UBound(csv) = 2 Then
        key = csv(0)
        item1 = csv(1)
        item2 = csv(2)
        If Not dic.exists(key) Then dic.Add csv(0), 0
        dic(key) = dic(key) + item1 + item2
    End If
Next

keys = dic.keys
items = dic.items

For i = 0 To UBound(keys)
    ans = ans & keys(i) & "," & items(i) & vbCrLf
Next

Set file = fso.OpenTextFile(filePath & fileName, ForWriting, True)
file.write ans
file.Close

'オブジェクト破棄
Set file = Nothing
Set dic = Nothing
Set fso = Nothing