I am new to VBScript and I am trying to create an array from some variables gathered from a text file. However, as a test, when I try to echo out the array, the first variable is blank and I have no idea why. Perhaps there is something in VBScript I'm not aware of. My code is as follows:
Set objFSO = CreateObject("Scripting.FileSystemObject")
objStartFolder = "filepath"
archivedCSV = "filepath"
xmlFolder = "filepath"
Set objFolder = objFSO.GetFolder(objStartFolder)
Set colFiles = objFolder.Files
For Each objFile in objFolder.Files
If LCase(objFSO.GetExtensionName(objFile.Name)) = "csv" Then
origName = objFile.Name
origPath = objFile.Path
objFile.Name = "temp.txt"
Set objFile = objFSO.OpenTextFile(objStartFolder & "\temp.txt", 1)
Dim callOffNo()
Dim orderNo()
n = 0
Do Until objFile.AtEndOfStream
strLine = objFile.ReadLine
arrFields = Split(strLine, ",")
If arrFields(0) <> "List of Call offs" Then
If arrFields(0) <> "Call Off No" Then
If arrFields(2) <> currentOrder Then
currentOrder = arrFields(2)
ReDim orderNo(n)
orderNo(n) = arrFields(2)
n = n + 1
End If
End If
End If
Loop
For Each order in orderNo
MsgBox order
Next
objFile.Close
newName = Replace(origName, objFSO.GetExtensionName(origPath), "")
archivedName = archivedCSV & "\" & origName
tempFile = objStartFolder & "\temp.txt"
objFSO.MoveFile tempFile, archivedName
Exit For
End If
Next
The txt file looks like this
List of Call offs,,,,
Call Off No,Customer,Order Number,Item No,Quantity
12345,COMPANY,1013,1234,1
12345,COMPANY,1013,8652,1
12345,COMPANY,1013,4652,1
12345,COMPANY,1013,7203,1
12345,COMPANY,1013,3365,1
67891,COMPANY,1020,8963,2
67891,COMPANY,1020,1326,2
Therefore, it should add two variables to the array: 1013, 1020. However, the first MsgBox is blank and the second says 1020.
I'm sure it's something simple that I am missing.
Dim arr(). EVER. It creates an array without an actual size, which means just asking for trouble. If you need a dynamic array useReDim arr(-1)for creating an empty array, or (better) useSystem.Collections.ArrayListobjects.