473,473 Members | 1,513 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Skipping lines in a text file

12 New Member
Hello. I have a scipt the reads and imports a text file into a database table. Below is an example of the text file I'm importing and the script I'm using to accomplish the import. You'll notice at the beginning of 3 of the lines in the file you see the number 84. What I need to be able to do is skip the first two lines where 84 appears and import the last line containing 84 and then continue to load the rest of the file. How can this be done?

Text File Example
"18",77,"Brinks Home","",08/01/2007,2146,2146,477,477,67,123,46,46,2,
"18",81,"United/Reliable Ins","",08/01/2007,762,762,241,241,136,238,0,0,0
"18",84,"Raleigh/Wake","",08/01/2007,1611,1611,68,68,17,93,0
"18",84,"Raleigh/Wake","",08/01/2007,694,694,75,75,11,10,0
"18",84,"Raleigh/Wake","",08/01/2007,2305,2305,143,143,28,
"18",88,"World Omni 88","",08/01/2007,1389,1389,525,525,
"18",89,"World Omni 89","",08/01/2007,142,142,20,20,3,7,47,47,
"18",99,"PSCU","",08/01/2007,1767,1767,2378,
"18",248,"Virginia Natural Gas","",08/01/2007,0,0,0,0,0,0,2,2,0,0,
"18",304,"Henrico County Real Estate","",08/01/2007,1,1,2,2,0,0,0,0,0,0,0,


Importing Script

Set objFile = CreateObject("Scripting.FileSystemObject")

Dim MyFile, MyFileNew
MyFile = Session("strFileName")

Set objText=objFile.OpenTextFile(MyFile)

RLog.Open "Select * From tblCoreImportTest",Conn,3,3

Do while not objText.AtEndOfStream

strTextLine=objText.Readline
data=Split(strTextLine,",")
Aug 7 '07 #1
3 1864
jhardman
3,406 Recognized Expert Specialist
the easiest way to do this is to make one more variable called something like currentRecord. Set currentRecord equal to strTextLine but don't save it to the db. Then go to the next line. If the part in question is still the same (data(1) = 84 on both strTextLine and currentRecord) then set currentRecord equal to the new strTextLine and move to the next record. If it is different, save the currentRecord to the db, then set currentRecord equal to strTextLine and move on. Does this make sense? You don't save any record until you verify that the next record has a different handle.

Jared
Aug 8 '07 #2
Anthony1312002
12 New Member
jhardman, thanks for responding. This really helps. I'm really just getting my mind around codeing concepts. I see you mentioned creating a variable and using this to ignore or skip certain lines in the file. I hope it's not asking to much but could you give me a visual of what this would look like using my current code?
Aug 17 '07 #3
jhardman
3,406 Recognized Expert Specialist
jhardman, thanks for responding. This really helps. I'm really just getting my mind around coding concepts. I see you mentioned creating a variable and using this to ignore or skip certain lines in the file. I hope it's not asking to much but could you give me a visual of what this would look like using my current code?
OK, I assume you already know how to save a record to the db, and you are just asking about how to skip, right? so try this:
Expand|Select|Wrap|Line Numbers
  1. Set objFile = CreateObject("Scripting.FileSystemObject")
  2.  
  3. Dim MyFile, MyFileNew, data, nextData
  4. MyFile = Session("strFileName")
  5.  
  6. Set objText=objFile.OpenTextFile(MyFile)
  7.  
  8. RLog.Open "Select * From tblCoreImportTest",Conn,3,3
  9.  
  10. strTextLine=objText.Readline
  11. data=Split(strTextLine,",")
  12. if NOT objText.EOS then
  13.    Do while not objText.AtEndOfStream
  14.       strTextLine = objText.Readline
  15.       nextData = Split(strTextLine, ",")
  16.  
  17.       if nextData(1) = data(1) then 'the next line has the same value for this field
  18.          'discard the old value without saving
  19.          data = nextData
  20.       else 'the next line has a different value
  21.          'save the old data, before updating
  22.          RLog.addNew
  23.          RLog("field1") = data(0)
  24.          RLog("field2") = data(1)
  25.          'etc
  26.          RLog.update
  27.          data = nextData
  28.       end if
  29.    loop
  30. end if
  31.  
  32. 'save the data one more time
  33. RLog.addNew
  34. RLog("field1") = data(0)
  35. RLog("field2") = data(1)
  36. 'etc
  37. RLOG.update
  38.  
I always make new records with addnew and update, but I assume you have your own way, go ahead and save the data any way you want. The important thing is that I get the first line of the text file into the "data" variable before I start the loop, then each time through the loop I first check to see if the field in question from the next line is the same as the last one. If it is the same I just set "data" equal to the next line without saving the old "data". If it is different, I save the old one, then set data equal to the next line. this will end up with the last line in variable "data" after you finish the loop, so you have to save one more time after you finish the loop. does this make sense?

Jared
Aug 17 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

22
by: Ling Lee | last post by:
Hi all. I'm trying to write a program that: 1) Ask me what file I want to count number of lines in, and then counts the lines and writes the answear out. 2) I made the first part like this: ...
3
by: Ivan Liu | last post by:
I would like know how I can skip a line while reading a set of input data (from a text file) if the first character of the line is "#". My original code reads: ifstream Infile("data.dat"); ...
6
by: lisa.engblom | last post by:
Hi, I've just started programming in python, and have run into an unexpected problem. I am using python to pull text data from some csv files. I have one file that has the important...
6
by: ivan.perak | last post by:
Hello, im a beginner in VB.NET... The thing i would like to do is as it follows.... I have a text file (list of names, every name to the next line) which is about 350000 lines long. I would...
7
by: peraklo | last post by:
Hello, there is another problem i am facing. i have a text file which is about 15000 lines big. i have to cut the last 27 lines from that file and create a new text file that contans those 27...
4
by: IraqiAli | last post by:
Is there a way to skip to a line of code in VB? Say i have something like this 1 If txtCandidates.Text = 5 Then 2 If txtCount1.Text > txtVotes.Text / 2 Then 3 MsgBox ("Candidate 1...
7
by: Gustaf | last post by:
Hi all, Just for fun, I'm working on a script to count the number of lines in source files. Some lines are auto-generated (by the IDE) and shouldn't be counted. The auto-generated part of files...
0
by: Jerry Coffin | last post by:
In article <4fae62b0-6858-4e9e-830e-9eecf6691d4a@ 59g2000hsb.googlegroups.com>, friend.blah@googlemail.com says... Each time you read from the file, keep track of the file position after...
4
by: BibI | last post by:
Hi there, I just started programming with PERL and am trying to put together my first little data manipulation program. I am working on a MAC with OSX. I have a data file with the following...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.