Results 1 to 4 of 4

Thread: textfile length

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2003
    Posts
    419

    textfile length

    Hello, I have a form with a textbox and a timer. The timer is set to an interval of 600. When the timer fires, it loads C:\messages.text into a textbox. How can I have it do the following:

    Check the linecount of the file, if it changed since the last time it opened..add the current time to the last line in the file. If that last line is blank...move back one until the current line isnt blank...then save the textfile.
    Last edited by LostAngel; Jul 18th, 2005 at 10:10 AM.
    Code:
    If LostAngel.Tag = "Programming" then
       LostAngel.Caption = "Awake"
    Else
       LostAngel.Caption = "Dreaming of Code"
    End If

  2. #2
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: textfile length

    VB Code:
    1. Private Sub Timer1_Timer()
    2.     Dim hFile As Integer, sLines() As String, sText As String
    3.     Static nLines As Long
    4.     On Error Resume Next
    5.     hFile = FreeFile
    6.     Open "c:\message.txt" For Binary As #hFile
    7.         sText = Input(LOF(hFile), hFile)
    8.     Close #hFile
    9.     sLines = Split(sText, vbCrLf)
    10.     If UBound(sLines) <> nLines Then
    11.         Do While Right$(sText, 2) = vbCrLf
    12.             sText = Left$(sText, Len(sText) - 2)
    13.         Loop
    14.         sText = sText & vbCrLf & Now
    15.         nLines = UBound(Split(sText, vbCrLf))
    16.         Open "c:\messages.txt" For Output As #hFile
    17.             Print #hFile, sText
    18.         Close #hFile
    19.     End If
    20.     If Text1.Text <> sText Then
    21.         Text1.Text = sText
    22.     End If
    23. End Sub

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2003
    Posts
    419

    Re: textfile length

    That code keeps adding the date & time to the textfile even when the file isnt changed...what i am wanting is for the program to detect when somethings added to the textfile...when its added...add the date/time to the end of the last line.
    Code:
    If LostAngel.Tag = "Programming" then
       LostAngel.Caption = "Awake"
    Else
       LostAngel.Caption = "Dreaming of Code"
    End If

  4. #4
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: textfile length

    Oh yeah of course! When the file is saved a new newline is added to it. Change the code to this:
    VB Code:
    1. Private Sub Timer1_Timer()
    2.     Dim hFile As Integer, sLines() As String, sText As String
    3.     Static nLines As Long
    4.     On Error Resume Next
    5.     hFile = FreeFile
    6.     Open "c:\message.txt" For Binary As #hFile
    7.         sText = Input(LOF(hFile), hFile)
    8.     Close #hFile
    9.     sLines = Split(sText, vbCrLf)
    10.     If UBound(sLines) <> nLines Then
    11.         Do While Right$(sText, 2) = vbCrLf
    12.             sText = Left$(sText, Len(sText) - 2)
    13.         Loop
    14.         sText = sText & vbCrLf & Now
    15.         nLines = UBound(Split(sText, vbCrLf))
    16.         Open "c:\messages.txt" For Output As #hFile
    17.             Print #hFile, sText[b][color=red];[/color][/b] '<- Added a semicolon here
    18.         Close #hFile
    19.     End If
    20.     If Text1.Text <> sText Then
    21.         Text1.Text = sText
    22.     End If
    23. End Sub

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