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!

Reading lines of delimited text file 1

Status
Not open for further replies.

JHugh

MIS
Aug 18, 2001
1
US
I'm new at this...

I'm reading a delimited text file, but I can't seem to figure out how to pull data between the delimiters out as variables in VBScript.
Ex.
The text file has 3 columns as such:

text1:text2:text3
text4:text5:text6

I need to read each line and pull each of the 3 "textx" fields out on each line and assign it a variable.
Ex.
var1 = text1
var2 = text2
var3 = text3

I'll loop back to read the next line and reassign the varibles.

Any pointers on how to do this?
Thanks!
 
This is an exercise for the pleasure of myself see if it helps. You can adapt to the way you use the data read.

The main ingredient is the function
Get_strdataArray_paged(dfile)
whereas the calling part of the script is for illustration purpose only.

The format of the data file is taken religiously and literally from your input. Any incomplete data entry is rendered by an empty string in the return array.

Play with it a bit to see the idea behind the construction.

regards - tsuji

'---------main--------------------
'This is an illustration of the use of Get_strdataArray_paged(datafile)

Option Explicit
Const datafile=".\datafile.txt" 'set yourself the path & filename
Const delimiter=":"
Const subset_size = 3

Dim strdArray
Dim no_of_set_of_data_present, ct1, ct2 , sdisplay

strdArray=Get_strdataArray_paged(datafile)

no_of_set_of_data_present = UBound(strdArray) \ subset_size

If (strdArray(0)="True") And (no_of_set_of_data_present > 0) Then
MsgBox "There are " & no_of_set_of_data_present & " set(s) of data, each of " _
& subset_size & " present."
For ct1 = 1 to no_of_set_of_data_present
sdisplay = "Data set # " & ct1 & vbCrLf
For ct2 = 1 to subset_size
sdisplay=sdisplay & strdArray((ct1-1)*subset_size+ct2) & vbCrLf
Next
MsgBox sdisplay
Next
End If
'---------End main-------

'/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
'Function Get_strdataArray returns a non-void an zero-based string Array
'Function Get_strdataArray_paged appends incomplete set of 3 with empty strings
'Parameter supplied dfile is the data file name including the necessary path and extension
'It should be a text file in construction with data delimiter ":"
'If reading of text file is successful Get_strdataArray_paged(0) flag is set to "True"
'If the reading fails, Get_strdataArray_paged(0) flag is set to "False"
'The data starts from Get_strdataArray_paged(1), if any.
'///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Function Get_strdataArray_paged(dfile)

Const ForReading = 1
Const ForWriting = 2
'Const delimiter = ":" 'Use global const set in the main
'Const subset_size = 3 'Use global const set in the main

Dim sdArray
Dim fso, txtStream
Dim text,dtake
Dim entry

Set fso=WScript.CreateObject("Scripting.FileSystemObject")
If Not fso.FileExists(dfile) Then
sdArray(0) = "False"
Else
dtake="True" & delimiter
Set txtStream=fso_OpenTextFile(dfile, ForReading)
Do While Not (txtStream.atEndOfStream)
text=trim(txtStream.Readline)
If len(text)<>0 Then
dtake=dtake & text & delimiter
End If
Loop
txtStream.Close
dtake=Left(dtake,len(dtake)-len(delimiter))
sdArray=split(dtake,&quot;:&quot;, -1,1)
Set txtStream = Nothing
End If
Set fso = Nothing

For entry = 0 to UBound(sdArray)
sdArray(entry)=trim(sdArray(entry))
Next

'------This is added to accommodate the expectation of each complete subset ----------
'------of data consists of three (3) data--------Get_strdataArray_paged----------------
Dim emptystring_to_append, appendcount
Dim sdArrayJoin

emptystring_to_append = UBound(sdArray) Mod subset_size

If emptystring_to_append <> 0 Then
sdArrayJoin = Join(sdArray,delimiter)
For appendcount = 1 to emptystring_to_append
sdArrayJoin=sdArrayJoin & delimiter
Next
sdArray=split(sdArrayJoin,delimiter)
End If
'-------------------------------------------------------------------------------------------

Get_strdataArray_paged = sdArray

End Function

'////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


 
Or, for each line:
myArray = Split(line, &quot;:&quot;)
then you can get the values using myArray(0), myArray(1) etc
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top