?
Solved

Reading in values from a text file using VBScript

Posted on 2006-04-20
2
Medium Priority
?
413 Views
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:

server=\\software
os=xp
model=d610

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
                Loop
             End If
0
Comment
Question by:mpenuel
2 Comments
 
LVL 12

Expert Comment

by:omegaomega
ID: 16505181
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.

Cheers,
Randy
0
 
LVL 2

Accepted Solution

by:
everlaast earned 2000 total points
ID: 16508376
Solution1:
use:
Dim arrInfoFinal(2)
in the beginning of the script


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


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

0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Suggested Courses
Course of the Month17 days, 5 hours left to enroll

862 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