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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ASP Reading a CSV File

Status
Not open for further replies.

Claypole

Programmer
Aug 22, 2002
12
0
0
GB
I need to be able to read a csv file.
A CSV is just a text file at the end of the day so have opened a TextStream
I need to know how I can split the line on the comma and populate the variables concerned before writing them to the database.

a typical line from the csv file would be as below
"4","South Birmingham High School","0121 687 4488"

I think i need to use InStr but can't find nuch about it am I on the correct track or is there a clearer method out there.

'Insert records from the CSV File
'Declare File access constant
Const ForReading = 1
Const TriStateFalse = 0

'Open the Text file Stream
set objFSO = server.CreateObject"Scripting.FileSystemObject")
'Using the getFile Method of objFSO,
'initialize the file Object
set filObject = objFSO.GetFile("c:\\edquest\updates\csv\CustomerRecords.csv")
'Use the OpenAsTextStream method to open the file
'for Reading and in Ascii format
filObject.OpenAsTextStream(ForReading,TriStateFalse)
'Loop around reading each line
do while not filObject.AtEndOfStream
'Use the ReadLine method to read the next line of text
'from the text file into the strBuffervariable
strBuffer = filObject.ReadLine
'Now split the variables at the comma

loop
filObject.Close
 
Interesting that you use the word split :)
Code:
Dim tArray   '<--- Addition

'Insert records from the CSV File
'Declare File access constant
Const ForReading = 1
Const TriStateFalse = 0
            
'Open the Text file Stream
set objFSO = server.CreateObject&quot;Scripting.FileSystemObject&quot;)
'Using the getFile Method of objFSO, 
'initialize the file Object
set filObject = objFSO.GetFile(&quot;c:\\edquest\updates\csv\CustomerRecords.csv&quot;)
'Use the OpenAsTextStream method to open the file 
'for Reading and in Ascii format
filObject.OpenAsTextStream(ForReading,TriStateFalse)
'Loop around reading each line
do while not filObject.AtEndOfStream 
  'Use the ReadLine method to read the next line of text
  'from the text file into the strBuffervariable
  strBuffer = filObject.ReadLine
  'Now split the variables at the comma
  tArray = Split(strBuffer,&quot;,&quot;)   '<--- Addition
  'Now read values of tArray

loop
filObject.Close
Using split will take a string and a delimiter and return an array of the values from the string. In the case of your example line above this would return:
tArray(0) = &quot;4&quot;
tArray(1) = &quot;South Birmingham High School&quot;
tArray(2) = &quot;0121 687 4488&quot;

A little easier than using InStr, hope that fits the bill
-Tarwn ------------ My Little Dictionary ---------
Extreme Programming - (1)Trying to code before my second cup of coffee. (2) While(1){ Ctrl+C; Ctrl+V; }
FAQ - Web-ese for &quot;Forget Asking Questions, I am to busy&quot; :p
 
Thanks for pointing me in the right direction.
The objects are causing a problem at the moment so haven't got as far as testing the split but i'm sure it will work.

I mentioned split because i'm predominantly a perl programmer and split is used within that.

Thanks Again
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top