Web Wiz - Green Windows Web Hosting

  New Posts New Posts RSS Feed - Reading and grabing data from a txt file
  FAQ FAQ  Forum Search   Events   Register Register  Login Login

Reading and grabing data from a txt file

 Post Reply Post Reply Page  12>
Author
zaboss View Drop Down
Senior Member
Senior Member


Joined: 20 August 2002
Location: Romania
Status: Offline
Points: 454
Post Options Post Options   Thanks (0) Thanks(0)   Quote zaboss Quote  Post ReplyReply Direct Link To This Post Topic: Reading and grabing data from a txt file
    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
Soft 4 web
Back to Top
Mart View Drop Down
Senior Member
Senior Member
Avatar

Joined: 30 November 2002
Status: Offline
Points: 2304
Post Options Post Options   Thanks (0) Thanks(0)   Quote Mart Quote  Post ReplyReply Direct Link To This Post 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

Back to Top
Semikolon View Drop Down
Senior Member
Senior Member


Joined: 09 September 2003
Location: Norway
Status: Offline
Points: 1718
Post Options Post Options   Thanks (0) Thanks(0)   Quote Semikolon Quote  Post ReplyReply Direct Link To This Post Posted: 04 June 2004 at 12:40pm
use FSO, Trim and Mid I think
Back to Top
zaboss View Drop Down
Senior Member
Senior Member


Joined: 20 August 2002
Location: Romania
Status: Offline
Points: 454
Post Options Post Options   Thanks (0) Thanks(0)   Quote zaboss Quote  Post ReplyReply Direct Link To This Post 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
Soft 4 web
Back to Top
Mart View Drop Down
Senior Member
Senior Member
Avatar

Joined: 30 November 2002
Status: Offline
Points: 2304
Post Options Post Options   Thanks (0) Thanks(0)   Quote Mart Quote  Post ReplyReply Direct Link To This Post 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)
%>

Back to Top
zaboss View Drop Down
Senior Member
Senior Member


Joined: 20 August 2002
Location: Romania
Status: Offline
Points: 454
Post Options Post Options   Thanks (0) Thanks(0)   Quote zaboss Quote  Post ReplyReply Direct Link To This Post 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
Soft 4 web
Back to Top
dpyers View Drop Down
Senior Member
Senior Member


Joined: 12 May 2003
Status: Offline
Points: 3937
Post Options Post Options   Thanks (0) Thanks(0)   Quote dpyers Quote  Post ReplyReply Direct Link To This Post 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.
Back to Top
zaboss View Drop Down
Senior Member
Senior Member


Joined: 20 August 2002
Location: Romania
Status: Offline
Points: 454
Post Options Post Options   Thanks (0) Thanks(0)   Quote zaboss Quote  Post ReplyReply Direct Link To This Post 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
Soft 4 web
Back to Top
 Post Reply Post Reply Page  12>

Forum Jump Forum Permissions View Drop Down

Forum Software by Web Wiz Forums® version 12.08
Copyright ©2001-2026 Web Wiz Ltd.


Become a Fan on Facebook Follow us on X Connect with us on LinkedIn Web Wiz Blogs
About Web Wiz | Contact Web Wiz | Terms & Conditions | Cookies | Privacy Notice

Web Wiz is the trading name of Web Wiz Ltd. Company registration No. 05977755. Registered in England and Wales.
Registered office: Web Wiz Ltd, Unit 18, The Glenmore Centre, Fancy Road, Poole, Dorset, BH12 4FB, UK.

Prices exclude VAT at 20% unless otherwise stated. VAT No. GB988999105 - $, € prices shown as a guideline only.

Copyright ©2001-2026 Web Wiz Ltd. All rights reserved.