How to Delete Specific String From a Flat File

Hello,
I have some code that reads each line of a fixed width flat file, and if a line is found where the length of the string 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\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

[1497 byte] By [cdun2] at [2007-12-27]
# 1

Here's something that will work

Dim strContents As String = My.Computer.FileSystem.ReadAllText("c:\test.txt")
Dim crlf() As String = {vbCrLf}
Dim line As String() = strContents.Split(crlf, StringSplitOptions.None)
Dim strOutput As String = ""
For Each s As String In line
If s.Length > 384 Then
strOutput = strOutput & s & vbCrLf
End If
Next
My.Computer.FileSystem.WriteAllText("C:\test.txt", strOuptut, False)

You need to write to a different file, you cant simply remove a line from the middle of an existing file. In this case the entire file is read in, each line is parsed creating a new string representing the new contents of the file and then the contents are written back, overwriting the original contents.

spotty at 2007-9-4 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 2

Thanks, I will try this!

cdun2

cdun2 at 2007-9-4 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 3

I've tried to design the code in the Script Task as follows;

Dim strContents As String = My.Computer.FileSystem.ReadAllText("C:\Learning\SettlementDataTest\SC15_Copies\SingleFile\CDNSC.CDNSC.SC00015.11062006")
Dim crlf() As String = {vbCrLf}
Dim line As String() = strContents.Split(crlf, StringSplitOptions.None)
Dim strOutput As String = ""


For Each Record As String In line
If Record.Length > 384 Then
strOutput = strOutput & Record & vbCrLf
End If
Next

My.Computer.FileSystem.WriteAllText("C:\Learning\SettlementDataTest\November6WrittenData.txt", strOutput, False)

line = Nothing
trOutput = Nothing

When I run the code within the SSIS designer, I get the following Runtime error;

Exeption of type 'System.OutOfMemoryExcpetion' was thrown.
at System.String.Split(String[] separator, Int32 count, StringSplitOptions options)
at System.String.Split(String[] separator, StringSplitOptions options)
at ScriptTask_8368ff2a02c84e0bb069567eb4ad0630.ScriptMain.Main()

What could be causing the error?

Thanks again!

cdun2 at 2007-9-4 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 4

How big is the file ?

Have you tried it with a small file for debugging / testing purposes.

spotty at 2007-9-4 > top of Msdn Tech,Visual Basic,Visual Basic Language...