Reading in values from a text file using VBScript

Posted on 2006-04-20
Last Modified: 2012-05-05
Hi, I am having some logic issues here and wanted to post this to see what I need to do.  I have a text file I'm reading in, the format of the text file is as follows:


What I need to do is grab "\\software", "xp", & "d610" and put them in variables to be used later.  The script echos "\\software" so it's reading the text file just fine.  When I try to store the values in an arry, I'm getting a "Type Mismatch" error.  The loop is the part I'm having trouble with.  Here's my code, please help!  Thanks.  Need more info, let me know.

            Set FSO = CreateObject("Scripting.FileSystemObject")
            If FSO.FileExists("Z:\servers\" & SvcTagFile) Then
                Const ForReading = 1, ForWriting = 2, ForAppending = 8
                Set objFile = FSO.OpenTextFile("Z:\servers\" & SvcTagFile, ForReading)
                x = 0
                Do While Not objFile.AtEndOfStream
                    arrInfo = objFile.Readline
                    arrInfo = Split(arrInfo, "=")
                    strInfo = arrInfo(1)
                    MsgBox strInfo 'It echos "\\software" like it should
         arrInfoFinal(x) = strInfo 'This is where the error is at
                    x = x + 1
             End If
Question by:mpenuel
    LVL 12

    Expert Comment

    Hello, mpenuel,

    I've never used VBScript, but if it behaves a little like VB then you will need to declare arrInfoFinal as an array before using it as you are.  Perhaps you have already done this in code not shown in your post.  But if not, try inserting:

                     Dim arrInfoFinal(2) As String

    before the start of the loop.  If there are more than 3 lines to parse then increase the upper bound of the array as required.  If you don't know the number of lines before reading the file, perhaps VBScript (like VB) has a "Redim Preserve" statement to allow you to increase the size dynamically.

    LVL 2

    Accepted Solution

    Dim arrInfoFinal(2)
    in the beginning of the script

    set arrInfoFinal=CreateObject("Scripting.Dictionary")
    in the beginning of the script

    find out more about dictionary object, it's a good replacement for array.


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Suggested Solutions

    Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
    This article will show, step by step, how to integrate R code into a R Sweave document
    The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
    The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now