[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 265
  • Last Modified:

Read Tab Delimited Text File with FileSystemObject in ASP


Hi,

I am trying to Read in the data from a Tab Delimited Text File Using ASP code.

The text file looks okay when I open it with Notepad but when I try to open with Excel, I have to go through the process of Importing the data with the excel wizard then it comes through okay, otherwise if I rename the file from .txt to .csv it comes through as one big value with the data seperated by little squares.

The problem I'm facing is that im using the FileSystemObject which reads a properly setup csv file from excel correctly and perfectly, but not a tab delimited text file.

Heres my code -

<%

dim sPath,objFso
strPath = Server.MapPath("MyFile.csv")
Set objFso = createObject("Scripting.FileSystemObject")
Set objTxtStream = objFso.OpenTextFile( strPath )

Dim strSQL
Dim i, myArr, theLine

While not objTxtStream.AtEndOfStream
theLine = objTxtStream.ReadLine

myArr = split(theLine, ",")
i = 0

str1      = myArr(i)
str2       = myArr(i+1)
str3      = myArr(i+2)
str4       = myArr(i+3)
str5      = myArr(i+4)
str6       = myArr(i+5)
str7       = myArr(i+6)
str8       = myArr(i+7)
str9       = myArr(i+8)
str10       = myArr(i+9)
str11       = myArr(i+10)
str12       = myArr(i+11)
str13       = myArr(i+12)
str14       = myArr(i+13)
str15       = myArr(i+14)
str16       = myArr(i+15)
str17       = myArr(i+16)
str18       = myArr(i+17)
str19       = myArr(i+18)
str20       = myArr(i+19)
str21       = myArr(i+20)
str22       = myArr(i+21)
str23       = myArr(i+22)
str24       = myArr(i+23)
str25       = myArr(i+24)

wend
                        
%>

Is there anyway I can read in a tab delimited text file with my code?

Thanks in advance!!!

Fracture



0
Fracture
Asked:
Fracture
  • 2
  • 2
1 Solution
 
boblahCommented:
Hi Fracture,

what happens if you change

myArr = split(theLine, ",")

to

myArr = split(theLine, chr(9))


Cheers!
0
 
FractureAuthor Commented:

Boblah!!!!

Thanks for your rapid reply, that worked great! I will award you the points, but just further to this - the file has headings and this is coming through as the first record.
I can get around this a few ways, like looping from the second record or seeing as i know what the headings are - just ignore them, but is there anyway of not getting this back as the first record?

Cheers again.

Fracture
0
 
boblahCommented:
Fracture,

changing

Dim strSQL
Dim i, myArr, theLine

While not objTxtStream.AtEndOfStream
theLine = objTxtStream.ReadLine

to

Dim strSQL
Dim i, myArr, theLine

objTxtStream.ReadLine

While not objTxtStream.AtEndOfStream
theLine = objTxtStream.ReadLine

would do the job, but that's pretty much what you suggested anyway. That would be preferable to knowing what they are and spotting them, which would not be very robust/maintainable.
0
 
FractureAuthor Commented:

Thanks bud, I had something similar but thats even better!

Cheers.

Fracture
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now