Results 1 to 8 of 8

Thread: Remove String From File

  1. #1

    Thread Starter
    Junior Member
    Join Date
    May 2005
    Posts
    17

    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

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Junior Member
    Join Date
    May 2005
    Posts
    17

    Re: Remove String From File

    Thanks for your help!
    cdun2

  4. #4

    Thread Starter
    Junior Member
    Join Date
    May 2005
    Posts
    17

    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.

  5. #5
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Re: Remove String From File

    This code reads and saves the file with out line 384.
    VB Code:
    1. 'Read all lines in to a string array and close the connection to the file.
    2.         Dim lines() As String = System.IO.File.ReadAllLines("Your file path")
    3.         'Declare streamWriter object and open file for write.
    4.         Dim sw As New System.IO.StreamWriter("Your file path")
    5.         Try
    6.             'Write the strings from "lines()" array to the file with out line 384.
    7.             For i As Integer = 0 To lines.GetUpperBound(0)
    8.                 If i <> 383 Then
    9.                     sw.WriteLine(lines(i))
    10.                 End If
    11.             Next
    12.         Catch ex As Exception
    13.             MessageBox.Show(ex.Message)
    14.         Finally
    15.             'close the file
    16.             sw.Close()
    17.         End Try
    18.  
    19.         'line 384 is still avalable for use.
    20.         Dim line384 As String
    21.         line384 = lines(383)

  6. #6
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: Remove String From File

    Quote Originally Posted by VBDT
    This code reads and saves the file with out line 384.
    VB Code:
    1. 'Read all lines in to a string array and close the connection to the file.
    2.         Dim lines() As String = System.IO.File.ReadAllLines("Your file path")
    3.         'Declare streamWriter object and open file for write.
    4.         Dim sw As New System.IO.StreamWriter("Your file path")
    5.         Try
    6.             'Write the strings from "lines()" array to the file with out line 384.
    7.             For i As Integer = 0 To lines.GetUpperBound(0)
    8.                 If i <> 383 Then
    9.                     sw.WriteLine(lines(i))
    10.                 End If
    11.             Next
    12.         Catch ex As Exception
    13.             MessageBox.Show(ex.Message)
    14.         Finally
    15.             'close the file
    16.             sw.Close()
    17.         End Try
    18.  
    19.         'line 384 is still avalable for use.
    20.         Dim line384 As String
    21.         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

  7. #7
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: Remove String From File

    Quote 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?

  8. #8
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    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:
    1. 'Read all lines in to a string array and close the connection to the file.
    2.         Dim lines() As String = System.IO.File.ReadAllLines("Your file path")
    3.         'Declare streamWriter objects and open the files for write.
    4.         Dim sw As New System.IO.StreamWriter("Your file path")
    5.         Dim sw1 As New System.IO.StreamWriter("Your new file path")
    6.         Try
    7.  
    8.             For Each line As String In lines
    9.                 If line.Length < 384 Then
    10.                     'Write the strings from "lines()" array to the file
    11.                     'whose length's are les then 384 chars.
    12.                     sw.WriteLine(line)
    13.                 Else
    14.                     'Write the strings from "lines()" array to the file
    15.                     'whose length's are >= 384 chars.
    16.                     sw1.WriteLine(line)
    17.                 End If
    18.             Next
    19.         Catch ex As Exception
    20.             MessageBox.Show(ex.Message)
    21.         Finally
    22.             'close the files
    23.             sw.Close()
    24.             sw1.Close()
    25.         End Try
    26.         'All lines are still avalable in the "lines()" array for use.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width