2

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.

1
  • 2
    Please do not use Dim arr(). EVER. It creates an array without an actual size, which means just asking for trouble. If you need a dynamic array use ReDim arr(-1) for creating an empty array, or (better) use System.Collections.ArrayList objects. Commented Dec 17, 2013 at 20:34

2 Answers 2

3

You need Preserve to 'grow' an array. A 'lonely' ReDim creates an array of empty slots; that's why your 'previous' elements are empty.

Sign up to request clarification or add additional context in comments.

Comments

0

You need to use preserve to change the size of an vbscript array while keeping the already existing elements.

The syntax works as follows:

ReDim Preserve yourArray(newSize)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.