Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Count Qty in String from Multiple Lines in Text File

Status
Not open for further replies.

Nu2Java

Technical User
Jun 5, 2012
166
US
Hello,

I am going to have hundreds of text files that I will be merging into a single text file. All of the files contain this line of text:

Code:
4-956170-10,Qty:,5,Dropped:,5/14/2014 1:59:36 PM,Completed:,5/14/2014 2:00:39 PM

How can I read the "5" next to Qty and total that up from the combined text file? Every line will be formatted the same aside of the date/time and part number in the beginning. I will be using this to total up the qty of parts produced per day.

I was hoping I could find a small script to do this instead of loading the file into Excel and doing it that way. Thanks in advance for any help.
 
What have you tried and where are you stuck?

Other posts you have made here show that you should be familiar with scrolling through a text file line-by-line, and using the Split function to split each line by the comma. So:

1) Scroll through the text file line by line
2) For each line, split by the comma
3) exract the 3rd element (actually its element 2 because arrays are zero-based. So, if you use [tt]arr = Split(line, ",")[/tt] then arr(2) would have the value of 5 based on your sample
4) keep a running total
 
Guitarzan, this is what I have so far.... but only gives me all the numbers. I can't quite figure out how to add each number and give the total.

Code:
Const ForReading = 1

Dim objFSO, objTextFile, arrTokens, intSum

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile("E:\Wave_LT\Test.txt", ForReading)

Do Until objTextFile.AtEndOfStream

strLine = objTextFile.ReadLine
arrTokens = split(strLine, ",")
intSum = intSum + arrTokens(2)

Loop

wscript.echo "Sum Total =  " & intSum
objTextFile.Close
 
I forget the small details sometimes....

Code:
intSum = 0
 
Correct, or you can use a conversion function to be sure of the data type:
[tt]intSum = intSum + CInt(arrTokens(2)[/tt] for integers, or use CDbl() if there are decimals.
 
Ahhh that is good to know. I will add that. Thanks Guitarzan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top