Appending Text Question

I am writing a program that searches for a certain criteria in a text file. Once it finds it, it appends a timestamp to the end of the line.

I have it to where it will append to the end of the file but not the end of the line.

I could use some help please.



  • seancampbellseancampbell Pennsylvania, USA
    I am not familiar with VB.Net's new version of file manipulation... However I do have a suggestion for you.

    Load the text file into memory 1 line at a time, after you analyze the line (and append the time stamp if needed) append the result to a String object that will contain the entire contents of the file...
    When you are done reading the file, close it, and write a new file over top of the old one... Here is a simple example (note this example does not have error handling):

    [This will search for the letter a in each line and append the date to the end of the line]
    'This finds an available 'file number' to use
    Dim FF As Integer = FreeFile()
    Dim StrLine As String 'This will hold a single line of text
    Dim StrFile As String 'This will hold the new file contents
    'This opens the file for input (reading)
    FileOpen(FF, "C:Log.txt", OpenMode.Input)

    'Do While Not EndOfFile
    Do While Not EOF(FF)
    StrLine = LineInput(FF) 'When the line is read, it removes the carrage return
    If StrLine.IndexOf("a") <> -1 Then
    StrLine &= " " & Format(Now, "MMddyyyy")
    End If
    StrFile &= StrLine & vbNewLine 'vbNewLine puts the carrage return back on

    'This opens the file for output (writing)
    FF = FreeFile()
    FileOpen(FF, "C:Log.txt", OpenMode.Output)
    PrintLine(FF, StrFile)
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


In this Discussion