|
-
Nov 29th, 2006, 05:35 PM
#1
Thread Starter
Junior Member
Remove String From File
Hello,
I have some code that reads each line of a fixed width flat file file, and if a line is found where the length of the string > 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\SC15_Copies\SingleFile\CDNSC.CDNSC.SC00015.11062006")
oWrite = oFile.CreateText("C:\Learning\SettlementDataTest\SC15_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 29th, 2006, 05:51 PM
#2
Re: Remove String From File
If you want to delete a line from a text file then you have to read the contents of the file in, remove the line, then write the new contents back out. To delete a line you simply don't write it out.
Note that you cannot write to a file that is open, so you have to either read the whole file in, close it, then write it out, or else write to a temporary file untill you've read the whole file, then close it and move the temp file to overwrite the original.
-
Dec 1st, 2006, 02:09 PM
#3
Thread Starter
Junior Member
Re: Remove String From File
Thanks for your help!
cdun2
-
Dec 1st, 2006, 02:57 PM
#4
Thread Starter
Junior Member
Re: Remove String From File
Actually, I still need to write the line out to another file, because I need to save the record where LEN > 384 to a seperate location. The record still needs to be removed from the source file.
-
Dec 1st, 2006, 03:21 PM
#5
Re: Remove String From File
This code reads and saves the file with out line 384.
VB Code:
'Read all lines in to a string array and close the connection to the file.
Dim lines() As String = System.IO.File.ReadAllLines("Your file path")
'Declare streamWriter object and open file for write.
Dim sw As New System.IO.StreamWriter("Your file path")
Try
'Write the strings from "lines()" array to the file with out line 384.
For i As Integer = 0 To lines.GetUpperBound(0)
If i <> 383 Then
sw.WriteLine(lines(i))
End If
Next
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
'close the file
sw.Close()
End Try
'line 384 is still avalable for use.
Dim line384 As String
line384 = lines(383)
-
Dec 1st, 2006, 03:44 PM
#6
Re: Remove String From File
 Originally Posted by VBDT
This code reads and saves the file with out line 384.
VB Code:
'Read all lines in to a string array and close the connection to the file.
Dim lines() As String = System.IO.File.ReadAllLines("Your file path")
'Declare streamWriter object and open file for write.
Dim sw As New System.IO.StreamWriter("Your file path")
Try
'Write the strings from "lines()" array to the file with out line 384.
For i As Integer = 0 To lines.GetUpperBound(0)
If i <> 383 Then
sw.WriteLine(lines(i))
End If
Next
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
'close the file
sw.Close()
End Try
'line 384 is still avalable for use.
Dim line384 As String
line384 = lines(383)
I thought he wants to remove the lines that are longer than 384 chars and save them to another file.... Not to remove the lines beyond line 384
-
Dec 1st, 2006, 03:44 PM
#7
Re: Remove String From File
 Originally Posted by cdun2
Actually, I still need to write the line out to another file, because I need to save the record where LEN > 384 to a seperate location. The record still needs to be removed from the source file.
Still need help on this?
-
Dec 1st, 2006, 03:58 PM
#8
Re: Remove String From File
Yes, Thanks stanav you are corrct. Thanks for pointing it out Here I changed the code so that it writes the lines whose Length’s are less then 384 chars in to the file, and writes the lines whose length’s are >= 384 chars in to the new file at the same time.
VB Code:
'Read all lines in to a string array and close the connection to the file.
Dim lines() As String = System.IO.File.ReadAllLines("Your file path")
'Declare streamWriter objects and open the files for write.
Dim sw As New System.IO.StreamWriter("Your file path")
Dim sw1 As New System.IO.StreamWriter("Your new file path")
Try
For Each line As String In lines
If line.Length < 384 Then
'Write the strings from "lines()" array to the file
'whose length's are les then 384 chars.
sw.WriteLine(line)
Else
'Write the strings from "lines()" array to the file
'whose length's are >= 384 chars.
sw1.WriteLine(line)
End If
Next
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
'close the files
sw.Close()
sw1.Close()
End Try
'All lines are still avalable in the "lines()" array for use.
Last edited by VBDT; Dec 2nd, 2006 at 12:31 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|