0

This is the code:

'creates the msxml object
Set xmlDoc = CreateObject("Msxml2.DOMDocument.6.0")
Dim retVal

'load the xml data of the script
retVal=xmlDoc.load(argFilePath)

Dim fso, folder, sFolder
Dim xmlDataPath, curNode

'get input folder
Set curNode=xmlDoc.selectSingleNode("//ScriptXmlData/inputFilePath")
Dim inputFolder, outputFolder, hotLoc
inputFolder=CSTR(curNode.text)

'location of jdf files
sFolder=inputFolder

'creating file getting object
Set fso = CreateObject("Scripting.FileSystemObject")
Set folder = fso.GetFolder(sFolder)

Dim amount, fName, arrC, i
i=0

'loop for getting amounts
For each folderIdx In folder.files
    If fso.GetExtensionName(folderIdx.Name) = "jdf" Then

        'increase array size
        Redim arrC(i)

        'get folder name
        fName=folderIdx.Name

        'get file path
        xmlDataPath = sFolder+"\"+fName

        'load the xml data of the script
        retVal = jdfDoc.load(xmlDataPath)

        'get amount
        Set curNode = jdfDoc.selectSingleNode("//jdf:JDF/jdf:ResourceLinkPool/jdf:ComponentLink")
        amount = curNode.getAttribute("Amount")

        'Create array that holds amount
        arrC(i)=amount
        i=i+1

    End If
Next

wscript.echo arrC(0)
wscript.echo arrC(1)
wscript.echo arrC(2)

Our problem is that once arrC exits the loop, some of its values go missing. For example, inside the loop our array is:

arrC(0)=100
arrC(1)=150
arrC(2)=200

Once it leaves the loop, as in the test in the end of the code, its values are :

arrC(0)=""
arrC(1)=""
arrC(2)=200

Can someone please explain?

Thank you!

1 Answer 1

2

ReDim doesn't preserve the elements of an array when used without the keyword Preserve. Change the line

Redim arrC(i)

into

Redim Preserve arrC(i)

You don't need an index variable, BTW. When you initialize the arrC as an empty array outside the loop, you can use the upper bound as your index:

ReDim arrC(-1)

For Each folderIdx In folder.files
  If fso.GetExtensionName(folderIdx.Name) = "jdf" Then
    ...
    ReDim Preserve arrC(UBound(arrC)+1)
    arrC(UBound(arrC)) = amount
  End If
Next

However, since ReDim Preserve actually creates a new array and copies the existing elements from the old one, it's bound to perform poorly with increasing array size. You may want to use an array list instead:

Set arrC = CreateObject("System.Collections.ArrayList")

For Each folderIdx In folder.files
  If fso.GetExtensionName(folderIdx.Name) = "jdf" Then
    ...
    arrC.Add amount
  End If
Next
Sign up to request clarification or add additional context in comments.

4 Comments

Very thorough!! Thank you very much you are great! Can you think of a way this would happen with a variable? Happened to us before but we were able to avoid it...
What kind of variable and in which context?
We created our own "Replace" function, which was in a loop. We put the result string in a variable. The loop ran 3 times, each replacing something else, then outside the loop the variable would lose its value
Might have been an issue with the scope of the variable. Hard to say without seeing the actual code.

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.