Reputation: 67
I am reading a txt file line by line, and searching for keywords. As I find a keyword, I am placing it in an array for later use. However I keep coming across problems with my dynamic array. Even if the txt file does not contain keywords, my array shows a couple of empty spaces and fills it up with empty strings. Am I filling up my dynamic array incorrectly? Here is a porting of my code:
Dim fso, inputFile, outputFile, notFound(), PortTransferError()
Const outPut = "out.txt"
Set fso = CreateObject("Scripting.FileSystemObject")
Set input = fso.OpenTextFile("file.txt")
Set outputFile = fso.CreateTextFile(outPut)
i = 0
Do Until input.AtEndOfStream
line = input.Readline
Redim Preserve notFound(i)
Redim Preserve PortTransferError(i)
If InStr(line, "Couldn't Find:") Then
notFoundError(i) = line
i = i + 1
ElseIf InStr(line, "Cannot Transfer to Port: ") Then
PortTransferErrorr(i) = line
i = i + 1
End If
Loop
input.Close
For each item in notFound
outputFile.Write(item) 'empty string vals in array
Next
For each item in PortTransferErrorr
outputFile.Write(item)
Next
Upvotes: 1
Views: 62
Reputation: 38745
You ReDim (and copy) the arrays for each input line and you grow both arrays for each of the conditions met; so move the ReDim lines to the appropriate Then parts.
Upvotes: 3