473,394 Members | 1,531 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Skipping lines in a text file

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",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",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

Aug 7 '07 #1
3 1862
3,406 Expert 2GB
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.

Aug 8 '07 #2
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
3,406 Expert 2GB
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")
  3. Dim MyFile, MyFileNew, data, nextData
  4. MyFile = Session("strFileName")
  6. Set objText=objFile.OpenTextFile(MyFile)
  8. RLog.Open "Select * From tblCoreImportTest",Conn,3,3
  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, ",")
  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
  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
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?

Aug 17 '07 #4

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

Similar topics

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: ...
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"); ...
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...
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...
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...
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...
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...
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...
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...
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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,...
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...
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.