Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Read text file into multidimensional array 1

Status
Not open for further replies.

jjason01

Programmer
Jul 7, 2010
7
US
I have a text file with a list of printer info.

IE:
Printername,ipaddress,location,drivername

I want to read each line into a multidimensional array so I can display the printer name and ip on one part of a HTA and the location and driver name on a different part. I can read in one line and split them into a array but I need to add all at the same time and be able to use any info at anytime. I am having a hard time working with the multidimensional arrays. Any help would be great!! Thanks
 
Thanks jges. I think that will work. Only one problem. When it reads in the ip address on my text file it does not display all of it. My header reads

Name,IPAddress,DriverPath,PCLDriverName,PSDriverName

I used the example from the msdn site and when I echo the IPAddress it comes back as 192.168 and is missing the rest of the IP. Below is my code any ideas??




On Error Resume Next
Const adOpenStatic = 3
Const adLockOptimistic = 3
Const adCmdText = &H0001

Set objConnection = CreateObject("ADODB.Connection")
Set objRecordSet = CreateObject("ADODB.Recordset")

strPathtoTextFile = "E:\Printer Install"

objConnection.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & strPathtoTextFile & ";" & _
"Extended Properties=""text;HDR=YES;FMT=Delimited"""

objRecordset.Open "SELECT * FROM prts.txt", _
objConnection, adOpenStatic, adLockOptimistic, adCmdText

Do Until objRecordset.EOF
Wscript.Echo "Printer Name: " & objRecordset.Fields.Item("Name")
Wscript.Echo "IP Address: " & objRecordset.Fields.Item("IPAddress")
Wscript.Echo "Driver INF: " & objRecordset.Fields.Item("DriverPath")
Wscript.Echo "PCL Name: " & objRecordset.Fields.Item("PCLDriverName")
Wscript.Echo "PS Name: " & objRecordset.Fields.Item("PSDriverName")
objRecordset.MoveNext
Loop

 
In the prts.txt file, surround the IP address with quotes (127.0.0.1 -> "127.0.0.1").
 
Thanks for you help. It sucks you have to put the quotes around the ip but it works!!
 
>It sucks you have to put the quotes around the ip but it works!!
For these problems related to schema ad hoc guessing, I come to the conclusion that using an external schema.ini is a better course to take, in particular if you are forced to physicall alter the data file without using schema.ini.

Make a text file, named by spec "schema.ini", and put it in the same directory as the data file(s). Suppose it is of a file name "xyz.csv". The content of the schema.ini looks something like this.
[tt]
[[blue]xyz.csv[/blue]]
Format=CSVDelimited
ColNameHeader=True
Col1=Name Text
Col2=IPAddress Text
Col3=DriverPath Text
Col4=PCLDriverName Text
Col5=PSDriverName Text
[/tt]
One schema.ini can serve multiple data files as long as they all be placed in the same directory, by piling up corresponding sections in schema.ini. So it is in the long run and short, not so much a burden in setting it up.
 
tsuji-
I don't follow your example of using a schema but I'm curious. I can only speculate on implementation. Care to ellaborate?

-Geates
 
For mulitdimensional arrays, I simply define key->value constants for use in arrays stored in a dictionary object.

file.ext
Code:
John,20,123 Street
Dan,29,700 Jackson
Mary,86,Gilmore Hills Retirment Home

readin.vbs
Code:
CONST Name = 0
CONST Age = 1
CONST Address = 2

dim arrPerson(2)

set dic = CreateObject("Scripting.Dictionary")
set objFSO = CreateObject("Scripting.FileSystemObject")
set objStream = objFSO.OpenTextFile ("file.ext", 1)

do while not objStream.AtEndOfStream
   arrPerson = split(objStream.ReadLine, ",")
   dic.Add arrPerson(Name), arrPerson
loop

arrPerson = dic.Item("Mary")
msgbox arrPerson(Address)
'Output: Gilmore Hills Retirment Home

Do you think I am going overboard using this method?
I just thought, what if the file.ext has a header? Is there a way to define const or variables with the name of what is read, like in PHP ($var = "pickle", $[red]$var[/red] creates a variable named "pickle")?

-Geates
 
There are numerous problems with this script.

[1] First, syntactically, declare arrPerson with fixed dimentsionis bound to error out with type mismatch. You've to declare it without that precision and let the engine cast it to the appropriate vartype.
[tt]
[red]'[/red]dim arrPerson(2)
dim arrPerson
[/tt]
[2] And then fundamentally, it is flawed. In the case shown, you always assume the first column be unique (or forcibly a kind of primary key or alike). That is not correct in general and no such assumption can be made.
 
i have to say if you are going to mess around with csv files through vbscript the recordset with the schema.ini file seems to be the most 'reusable'.

i have never had to deal with csv files, and when i have i have been in excel.

it is of interest to see the use of ado for these types of things and i always pinch the examples for my own lib.

thanks tsuji for the schema.ini example
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top