Results 1 to 7 of 7

Thread: [RESOLVED] clearing a text file

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2006
    Posts
    104

    Resolved [RESOLVED] clearing a text file

    I have this code that reads a logfile and stores the lastline as a string in "lastline"
    It then checks to see if the log file is >= 10 lines long and if it is opens a srteamwrither so it clears the logfile. I do this because the log file can get very long and i figured the program will run faster if it only has to read 10 lines at the most form the file rather then 400. the problem im having is it gives me an error saying theat the file is in use by another process. I dont get this error if i remove the code for the streamwriter that clears the file, why is this?
    -------------------------------------------------------------------------

    Public Shared Sub logreader(ByVal source As Object, ByVal e As System.IO.FileSystemEventArgs)
    If e.ChangeType = IO.WatcherChangeTypes.Changed Then

    Dim p As Integer


    p = 0
    currentline = ""

    Dim fs As New System.IO.FileStream(CurrentEq2LogFile, IO.FileMode.OpenOrCreate, IO.FileAccess.ReadWrite, FileShare.ReadWrite)
    Dim sr As New System.IO.StreamReader(fs)


    Do While p <> -1
    lastline = ""

    'reads the log file line by line until the end of the log reached
    If sr.Peek <> -1 Then
    currentline = sr.ReadLine 'holds the last line read from the log file
    line = line + 1
    End If



    If sr.Peek = -1 Then
    p = -1
    lastline = currentline
    End If
    Loop
    fs.Close()
    sr.Close()
    Call praselog()' this sub checks to see if last line contains a certant word
    End If
    Call clearlog()

    End Sub

    Public Shared Sub clearlog()

    If line >= 10 Then
    Dim clearfile As New System.IO.StreamWriter(CurrentLogFile)

    line = 0
    clearfile.Close()
    End If

    End Sub

  2. #2
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    Re: clearing a text file

    What is in your praselog sub? Chances are you are leaving a streamreader or streamwriter open on that file there or some other place, so it cant open a new one on that file because you haven't called close on the previous one.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Mar 2006
    Posts
    104

    Re: clearing a text file

    the praselog sub just has a few if then statements there arent any streams.

  4. #4
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    Re: clearing a text file

    You can probably simpify this code by doing something like...
    VB Code:
    1. 'loops until end of file
    2. While sr.Peek <> -1
    3. 'code here
    4. End While
    ***Removed post below since I didnt see your end loop line. You can put your code in [ vbcode ] [ /vbcode ] tags so it can be more readable
    Last edited by gigemboy; Apr 9th, 2006 at 09:06 AM.

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Mar 2006
    Posts
    104

    Re: clearing a text file

    Ok thanx gigemboy I did what you sugested and you were right thanx!
    Not only does it work now but my code is much less confusing too lol.
    here is the new code.
    -----------------------------------------------------------------------
    VB Code:
    1. Public Shared Sub logreader(ByVal source As Object, ByVal e As System.IO.FileSystemEventArgs)
    2.         If e.ChangeType = IO.WatcherChangeTypes.Changed Then ' the following code will only execute if file is changed
    3.            
    4.  
    5.             Dim fs As New System.IO.FileStream(CurrentLogFile, IO.FileMode.OpenOrCreate, IO.FileAccess.ReadWrite, FileShare.ReadWrite)
    6.             Dim sr As New System.IO.StreamReader(fs)
    7.  
    8.             Do While sr.Peek <> -1
    9.                 lastline = sr.ReadLine' lastline is a string that holds the last line of the log file
    10.                 line = line + 1' counts how many lines were read
    11.             Loop
    12.  
    13.             fs.Close()
    14.             sr.Close()
    15.             fs.Dispose()
    16.             sr.Dispose()
    17.  
    18.         End If
    19.  
    20.         Call Cleanlog()
    21.         Call checkiflastlinecontains()
    22.  
    23.     End Sub
    24.  
    25.     Public Shared Sub cleanlog()
    26.  
    27.         If line >= 10 Then
    28.             filewatcher.EnableRaisingEvents = False 'stops the logreader sub event monitor
    29.             Dim sw As New System.IO.StreamWriter(CurrentLogFile, False)
    30. 'writes a blank log file
    31.             line = 0
    32.             sw.Close()
    33.             sw.Dispose()
    34.             filewatcher.EnableRaisingEvents = True ' restarts the logreader event monitor
    35.         Else : line = 0
    36.         End If
    37.  
    38.     End Sub
    Last edited by chaos75; Apr 9th, 2006 at 09:40 AM.

  6. #6
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    Re: [RESOLVED] clearing a text file

    I think you can refrain from calling .Dispose after calling .Close on the Streamreaders and StreamWriters, because I believe the .Close method disposes of the object as well (I have never had to explicitly call .Dispose on it).

  7. #7
    Junior Member
    Join Date
    Jan 2007
    Posts
    29

    Re: [RESOLVED] clearing a text file

    I'm running into kind of the same thing.

    Same situation. If there are changes I streamread the text file and read the contents into a textbox and trigger different actions based on what is is read.

    i do the
    read.close()

    My problem is that the thread does not close in time to open the reader again the next time the routine fires. If i pause 20 seconds after the reader.close() it works fine. Otherwise the file is still locked.

    I want it to read as fast as the file changes....is there any other way than with streamreader to do this?

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