Vikx One
asked on
sum up value from files header using .vbscript
I have two files (with same format) with header. in the file header, it contains the filedate, filename,total number of row & total amount of data. (header format sample: 20160912|filename| 312 |4019)
so i need to create a script that will merge these two files & will create new file. my problem is at the header part, i cant seem to get how to add/sum the value of number of row & total amount of data from these two files:
let says:
File 1 :
20160912|filename| 312 |4019
File 2:
20160912|filename| 100 |21)
Output File:
20160912|filename| 412 |4040)
Please help. So far, i have this build up:
so i need to create a script that will merge these two files & will create new file. my problem is at the header part, i cant seem to get how to add/sum the value of number of row & total amount of data from these two files:
let says:
File 1 :
20160912|filename| 312 |4019
File 2:
20160912|filename| 100 |21)
Output File:
20160912|filename| 412 |4040)
Please help. So far, i have this build up:
Const ForReading = 1
Const separator = "|"
Const columnAmount = 3
FixedHeaderPart = "20160204" & separator & "FILENAME" & separator
strPath = "D:\Data\script\"
strNameFile1 = "File1.txt"
strNameFile2 = "File2.txt"
strNameOutFile = "MyFile.txt"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile(strPath & strNameFile1, ForReading)
strTemp = ""
intRowCounter = 0
intSum = 0
' throw away the first line, as it can only created at the end of this merger operation
objTextFile.ReadLine
Do Until objTextFile.AtEndOfStream
strLine = objTextFile.ReadLine
IntRowCounter = intRowCounter + 1
' split up the line and add the amount in column 7 to the sum
intAmount=split(strLine,separator)(columnAmount)
intSum = intSum + intAmount
strTemp = strTemp & strLine & vbCrLf
Loop
objTextFile.Close
Set objTextFile = objFSO.OpenTextFile(strPath & strNameFile2, ForReading)
' throw away the first line, as it can only created at the end of this merger operation
objTextFile.ReadLine
Do Until objTextFile.AtEndOfStream
strLine = objTextFile.ReadLine
intRowCounter = intRowCounter + 1
' split up the line and add the amount in column 7 to the sum
intAmount=split(strLine,separator)(columnAmount)
intSum = intSum + intAmount
strTemp = strTemp & strLine & vbCrLf
Loop
objTextFile.Close
Set objOutputFile = objFSO.CreateTextFile(strNameOutFile)
objOutputFile.WriteLine FixedHeaderPart & intRowCounter & separator & intSum
objOutputFile.Write strTemp
objOutputFile.Close
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Open in new window