473,320 Members | 1,859 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,320 software developers and data experts.

Delete Selected Lines from Text File

Hello,
I have some code that reads each line of a text file, and if a line is
found where the length of the string in the line is 384, it writes
the line to a text file.

The other step that I need to take is to delete the line from the
source file. The code is as follows;

-*****************************************
Public Sub Main()
'find the records where the string length is greater than 384
'write them out to a file
'delete them from the source file
Dim oFile As System.IO.File
Dim oRead As System.IO.StreamReader
Dim oWrite As System.IO.StreamWriter
Dim LineIn As String

oRead =
oFile.OpenText("C:\Learning\SettlementDataTest\SC1 5_Copies\SingleFile\CDNSC.CDNSC.SC00015.11062006")
oWrite =
oFile.CreateText("C:\Learning\SettlementDataTest\S C15_Copies\ErrantRecords\ErrantRecords.txt")
While oRead.Peek <-1
LineIn = oRead.ReadLine()
If Len(LineIn) 384 Then
oWrite.WriteLine(LineIn)
'what do I do here to delete the 'LineIn' from the
source file?

End If
End While

oRead.Close()
oWrite.Close()
oFile = Nothing
LineIn = Nothing
Dts.TaskResult = Dts.Results.Success
End Sub
-*************************************
What would I do to delete the 'LineIn' found in the condition? By the
way, I'm doing this in a Script Task of a Integration Services package.

Thank you for your help!

cdun2

Nov 29 '06 #1
2 3517

You need to be writing a temporary file from the source file, missing out
the lines you want to delete. When you've finished, delete the original
file and rename the temporary file.
Nov 29 '06 #2
cdun2 wrote:
I have some code that reads each line of a text file, and if a line is
found where the length of the string in the line is 384, it writes
the line to a text file.

The other step that I need to take is to delete the line from the
source file. The code is as follows;
You /cannot/ delete lines from a sequential disk file. How do you
expect VB go about shunting all the remaining lines backwards on the disk?

Create two output files, one for the filtered lines, one for the rest
and, at the end of processing overwrite the source file with the latter.

Dim oIn as New StreamReader( "in.txt" )
Dim oOut1 As New StreamWriter( "out1.txt" )
Dim oOut2 As New StreamWriter( "out2.txt" )

Dim sRecord As String _
= oIn.ReadLine()
Do While Not ( sRecord Is Nothing )
If sRecord.Length 384 Then
oOut1.WriteLine( sRecord )
Else
oOut2.WriteLine( sRecord )
End If
sRecord = oIn.ReadLine()
Loop
oOut1.Close()
oOut2.Close()
oIn.Close()

File.Copy( "out2.txt", "in.txt", True )

HTH,
Phill W.
Nov 30 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: deko | last post by:
I'm not sure how to do this in php - need to calculate and delete an unspecified number of lines from the *top* of a file. <?php //append $visitor to the bottom of $visdata $fp =...
16
by: Philip Boonzaaier | last post by:
I want to be able to generate SQL statements that will go through a list of data, effectively row by row, enquire on the database if this exists in the selected table- If it exists, then the colums...
2
by: Murali | last post by:
Hello Everyone, I am breaking my head with this problem. Please help me out. Let me first explain my problem : Here it is: I am working in realtime environment where i will be creating...
3
by: Arpan | last post by:
A Form has a FileUpload, 2 Buttons & a TextBox web server controls. Using the FileUpload control, I want to give users the provision to move & delete files that DO NOT exist in C:\Inetpub\wwwroot...
2
by: jasone | last post by:
Hi, A section of my database allows users to delete items from a table, there is only one collumn in the table, this contains module information(parts of a manufacturing line) due to the lines...
24
by: biganthony via AccessMonster.com | last post by:
Hi, I have the following code to select a folder and then delete it. I keep getting a Path/File error on the line that deletes the actual folder. The line before that line deletes the files in...
3
by: Barkingmadscot | last post by:
I am stuck, i can workout how to remove lines from an array I have loading a text file (a Log), I know which lines a need, but the logs can be upto 30K sometimes bigger. I found trying to...
6
by: Annalyzer | last post by:
My database must produces a .csv file with a header line that is different from the detail lines. This .csv file contains a monthly report to an outside agency and is uploaded online so it must...
29
by: shivasusan | last post by:
Hi! I can add rows with inputs to my HTML table dynamically using DOM, but I cannot remove selected rows. In fact, every row contains a Delete button. So, user selects the rows to remove, clicks...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.