Reading and grabing data from a txt file
Printed From: Web Wiz Forums
Category: General Discussion
Forum Name: Classic ASP Discussion
Forum Description: Discussion on Active Server Pages (Classic ASP).
URL: https://forums.webwiz.net/forum_posts.asp?TID=10731
Printed Date: 31 March 2026 at 1:36am Software Version: Web Wiz Forums 12.08 - https://www.webwizforums.com
Topic: Reading and grabing data from a txt file
Posted By: zaboss
Subject: Reading and grabing data from a txt file
Date Posted: 04 June 2004 at 10:52am
I need to read and grab some data from a text file, then insert it in
db. The text file is structured in 5 sections, and each section must be
added in a different table in the db. The text file looks like this:
[section 1]
1st row of data
2nd row
3rd row
etc... (no fixed number).
[end section 1]
[section 2]
1st row of data
2nd row
3rd row
etc... (no fixed number).
[end section 2]
....
the same for all sections
|
Can somebody point me in a right direction? And also, which is the equivalent of tab key? (the data is tab delimited).
------------- Cristian Banu
http://www.soft4web.ro - Soft 4 web
|
Replies:
Posted By: Mart
Date Posted: 04 June 2004 at 11:29am
|
Point you in the right direction for what? Reading a text file or parsing it?
Also it will probably be 10x easier if you used XML
|
Posted By: Semikolon
Date Posted: 04 June 2004 at 12:40pm
|
use FSO, Trim and Mid I think
|
Posted By: zaboss
Date Posted: 04 June 2004 at 1:00pm
Can zou be more specific, please? On how to read the file, grab the
sections put them in an array. Then, I know how to put them in the db
using Split(arrayelement, VBtab).
Some sample code would be great.
------------- Cristian Banu
http://www.soft4web.ro - Soft 4 web
|
Posted By: Mart
Date Posted: 04 June 2004 at 2:22pm
|
Here is how to read a file:
<% Set fs = CreateObject("Scripting.FileSystemObject")
Set wfile = fs.OpenTextFile("c:\Mydir\myfile.txt") filecontent = wfile.ReadAll
wfile.close Set wfile=nothing Set fs=nothing
response.write(filecontent) %>
|
Posted By: zaboss
Date Posted: 04 June 2004 at 7:43pm
OK, I have something working here:
<%@LANGUAGE="VBSCRIPT" CODEPAGE="1252"%>
<!--#include file="connect.asp"-->
<%
Set FSO = Server.CreateObject("Scripting.FileSystemObject")
Set theTextFile = FSO.OpenTextFile("E:\webs\ranbaxyromania.ro\wwwroot\files\tb lViziteDoctori1.txt")
theTextLine=theTextFile.ReadLine
theFields=Split(theTextLine, VBTab)
While Not theTextFile.AtEndOfStream
mSQL = "INSERT INTO tblViziteDoctori("
mSQL = mSQL & "strDoctor,"
mSQL = mSQL & "strSpecializare,"
mSQL = mSQL & "strJointWorkWith,"
mSQL = mSQL & "strInput,"
mSQL = mSQL & "strFeedback,"
mSQL = mSQL & "strData,"
mSQL = mSQL & "strOra,"
mSQL = mSQL & "strInput2,"
mSQL = mSQL & "strProdusPrezentat,"
mSQL = mSQL & "strProdusPrezentat2,"
mSQL = mSQL & "strObservatii)"
mSQL = mSQL & "Values("
mSQL = mSQL & "'" & Replace(thefields(8),"'","''") & "', "
mSQL = mSQL & "'" & Replace(thefields(4),"'","''") & "', "
mSQL = mSQL & "'" & Replace(thefields(3),"'","''") & "', "
mSQL = mSQL & "'" & Replace(thefields(1),"'","''") & "', "
mSQL = mSQL & "'" & Replace(thefields(2),"'","''") & "', "
mSQL = mSQL & "'" & Replace(thefields(6),"'","''") & "', "
mSQL = mSQL & "'" & Replace(thefields(9),"'","''") & "', "
mSQL = mSQL & "'" & Replace(thefields(10),"'","''") & "', "
mSQL = mSQL & "'" & Replace(thefields(11),"'","''") & "', "
mSQL = mSQL & "'" & Replace(thefields(12),"'","''") & "', "
mSQL = mSQL & "'" & Replace(thefields(7),"'","''") & "')"
'It executes the SQL
MyConn.Execute (mSQL)
' Now we loop to the next record and start again until the end of the file.
Wend
' Close up to be tidy
theTextFile.Close
Set theTextFile = Nothing
MyConn.Close
Set MyConn = Nothing
%>
|
The BIG problem is that i reads ONLY the first line and insert it in
the db until it dies on natural causes (the script time out). That is
about 63.000 times :lol: :lol: :lol: What am I doing
wrong.
------------- Cristian Banu
http://www.soft4web.ro - Soft 4 web
|
Posted By: dpyers
Date Posted: 04 June 2004 at 9:34pm
|
You never go to the next line... Move
theTextLine=theTextFile.ReadLine theFields=Split(theTextLine, VBTab) |
After
While Not theTextFile.AtEndOfStream |
-------------
Lead me not into temptation... I know the short cut, follow me.
|
Posted By: zaboss
Date Posted: 05 June 2004 at 2:38am
Thanks, that was it! Now, it remains the other part of the task:
finding the part between sections and applying the right sql to each
one:
[section 1]
........
[/section 1]
[section 2]
....
[/section2]
.....
[section X]
....
[/end section x]
|
------------- Cristian Banu
http://www.soft4web.ro - Soft 4 web
|
Posted By: Mart
Date Posted: 05 June 2004 at 5:02am
|
Howcome you arn't using XML for this? It will be quite hard to do what you are trying to do and most likely pointless if you could do it with XML
|
Posted By: zaboss
Date Posted: 05 June 2004 at 12:59pm
I can't use xml. The text file is exported by an Access application,
and I have to deal with it. Also, the best way to something is the way
you know it. I never use xml, so I never though of it as a possible
solution.
------------- Cristian Banu
http://www.soft4web.ro - Soft 4 web
|
Posted By: zaboss
Date Posted: 06 June 2004 at 5:00pm
Ok, i have tryed to put the rows in an array, then use the
Split(Rowarray(i), "[section]") to get the rows in a section, then put
them in an array to get the fields, nut with no luck. Any hints?
------------- Cristian Banu
http://www.soft4web.ro - Soft 4 web
|
Posted By: zaboss
Date Posted: 07 June 2004 at 10:03am
Well, it seems there is no need for arrays... I have sorted it partially. I have put there only the response.write to simplify.
<%@LANGUAGE="VBSCRIPT" CODEPAGE="1252"%>
<!--#include file="connect.asp"-->
<%
Set FSO = Server.CreateObject("Scripting.FileSystemObject")
Set theTextFile = FSO.OpenTextFile("E:\webs\ranbaxyromania.ro\wwwroot\files\tb lViziteDoctori1.txt")
While Not theTextFile.AtEndOfStream
theTextLine=theTextFile.ReadLine
If theTextline = "[section 1]" then
do
theTextLine=theTextFile.ReadLine
response.Write theTextLine & "<br>"
loop Until theTextLine = "[section 2]"
End if
If theTextline = "[section 2]" then
do
theTextLine=theTextFile.ReadLine
response.write theTextLine & "<br>"
loop Until theTextLine = "[section 3]"
End if
Wend
' Close up to be tidy
theTextFile.Close
Set theTextFile = Nothing
MyConn.Close
Set MyConn = Nothing
%>
|
The problem is that it lists also the section delimeter, and this crushes the insert. How can I skip this line?
------------- Cristian Banu
http://www.soft4web.ro - Soft 4 web
|
Posted By: dpyers
Date Posted: 07 June 2004 at 1:28pm
|
If you're referring to the End-Of-Section delimiter, try changing the loop until the end of the section rather than the beginning of the new section - e.g.
Change Loop Until theTextLine = "[section 2]" To Loop Until theTextLine = "[/section 1]"
-------------
Lead me not into temptation... I know the short cut, follow me.
|
Posted By: zaboss
Date Posted: 07 June 2004 at 1:33pm
Sorted out.
<%@LANGUAGE="VBSCRIPT" CODEPAGE="1252"%>
<%
Set FSO = Server.CreateObject("Scripting.FileSystemObject")
Set theTextFile = FSO.OpenTextFile("E:\webs\ranbaxyromania.ro\wwwroot\files\tb lViziteDoctori1.txt")
While Not theTextFile.AtEndOfStream
theTextLine=theTextFile.ReadLine
If theTextline = "[section 1]" then
do
theTextLine=theTextFile.ReadLine
' Only Write it out if it isn't a section line
If theTextLine<>"[section 2]" Then
response.Write theTextLine & "<br>"
End If
loop Until theTextLine = "[section 2]"
End if
If theTextline = "[section 2]" then
do
theTextLine=theTextFile.ReadLine
' Only Write it out if it isn't a section line
If theTextLine<>"[section 3]" Then
response.write theTextLine & "<br>"
End IF
loop Until theTextLine = "[section 3]"
End if
Wend
' Close up to be tidy
theTextFile.Close
Set theTextFile = Nothing
%>
|
The problem was that, instead of If theTextLine<>"[section 2]", i
was putting If theTextLine="[section 2]", which, of course, gave me an
error...
------------- Cristian Banu
http://www.soft4web.ro - Soft 4 web
|
Posted By: snapey
Date Posted: 18 June 2004 at 3:45pm
|
Mart wrote:
Howcome you arn't using XML for this? It will be quite hard to do what you are trying to do and most likely pointless if you could do it with XML |
I want to do similar, but using XML. Can anyone point me in the direction of any examples of reading an XML file into an array or database in classic ASP?
|
|