Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Append text file

Append text file

Scheduled Pinned Locked Moved Visual Basic
help
6 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    jds1207
    wrote on last edited by
    #1

    I have a sub procedure that cleans a text file and write it to the console. Now I need some help appending that cleaned text file to its specified path. Here is the code that I have: Private Sub CleanFile() 'Read file line by line Dim file_name As String = "c:\Scanning files\lsprint_MO.txt" Dim textline As String If System.IO.File.Exists(file_name) = True Then Dim objReader As New System.IO.StreamReader(file_name) Do While objReader.Peek() - 1 textline = objReader.ReadLine() & vbNewLine If textline.IndexOf("xxxxxxx") = 17 Then Console.WriteLine(textline) WriteFile(textline) End If Loop End If End Sub Private Sub WriteFile(ByVal fname As String) Dim oWrite As System.IO.StreamWriter oWrite = IO.File.AppendText("c:\Scanning files\lsprint_MO.txt") End Sub When I run the program I get the following message: The process cannot access the file 'c:\Scanning files\lsprint_MO.txt' because it is being used by another process. Any ideas! jds1207

    M 1 Reply Last reply
    0
    • J jds1207

      I have a sub procedure that cleans a text file and write it to the console. Now I need some help appending that cleaned text file to its specified path. Here is the code that I have: Private Sub CleanFile() 'Read file line by line Dim file_name As String = "c:\Scanning files\lsprint_MO.txt" Dim textline As String If System.IO.File.Exists(file_name) = True Then Dim objReader As New System.IO.StreamReader(file_name) Do While objReader.Peek() - 1 textline = objReader.ReadLine() & vbNewLine If textline.IndexOf("xxxxxxx") = 17 Then Console.WriteLine(textline) WriteFile(textline) End If Loop End If End Sub Private Sub WriteFile(ByVal fname As String) Dim oWrite As System.IO.StreamWriter oWrite = IO.File.AppendText("c:\Scanning files\lsprint_MO.txt") End Sub When I run the program I get the following message: The process cannot access the file 'c:\Scanning files\lsprint_MO.txt' because it is being used by another process. Any ideas! jds1207

      M Offline
      M Offline
      Martin Smith
      wrote on last edited by
      #2

      Hi, You are seeing this problem because the WriteFile() function is trying to open a stream to the same file opened in the CleanFile() function. A quick and easy solution is to create a temporary output file and pass its filename into the WriteFile() function, then at the end of the CleanFile() function delete the original c:\Scanning files\lsprint_MO.txt file and then rename the temporary file... You can get a temporary file name by: dim Temp_file as String = my.Computer.FileSystem.GetTempFileName() Then update WriteFile() function: Private Sub WriteFile(ByVal OUTPUT_FILE as String, ByVal fname As String) Dim oWrite As New System.IO.StreamWriter(OUTPUT_FILE) oWrite = IO.File.AppendText(fname) End Sub At the end of CleanFile() ObjReader.Close() My.Computer.FileSystem.DeleteFile("c:\Scanning files\lsprint_MO.txt") My.Computer.FileSystem.RenameFile(Temp_file, "c:\Scanning files\lsprint_MO.txt") I hope this helps you out.... Regards Martin

      J 1 Reply Last reply
      0
      • M Martin Smith

        Hi, You are seeing this problem because the WriteFile() function is trying to open a stream to the same file opened in the CleanFile() function. A quick and easy solution is to create a temporary output file and pass its filename into the WriteFile() function, then at the end of the CleanFile() function delete the original c:\Scanning files\lsprint_MO.txt file and then rename the temporary file... You can get a temporary file name by: dim Temp_file as String = my.Computer.FileSystem.GetTempFileName() Then update WriteFile() function: Private Sub WriteFile(ByVal OUTPUT_FILE as String, ByVal fname As String) Dim oWrite As New System.IO.StreamWriter(OUTPUT_FILE) oWrite = IO.File.AppendText(fname) End Sub At the end of CleanFile() ObjReader.Close() My.Computer.FileSystem.DeleteFile("c:\Scanning files\lsprint_MO.txt") My.Computer.FileSystem.RenameFile(Temp_file, "c:\Scanning files\lsprint_MO.txt") I hope this helps you out.... Regards Martin

        J Offline
        J Offline
        jds1207
        wrote on last edited by
        #3

        I got this error message: Illegal characters in path. This line was highlighted: Dim oWrite As New System.IO.StreamWriter(Output_File) Also, am I on the right track? Private Sub CleanFile() 'Read file line by line Dim file_name As String = "c:\Scanning files\lsprint_MO.txt" Dim textline As String Dim Temp_file As String = My.Computer.FileSystem.GetTempFileName If System.IO.File.Exists(file_name) = True Then Dim objReader As New System.IO.StreamReader(file_name) Do While objReader.Peek() - 1 textline = objReader.ReadLine() & vbNewLine If textline.IndexOf("xxxxxxx") = 17 Then 'Console.WriteLine(textline) WriteFile(textline, "c:\Scanning files\lsprint_MO.txt") End If Loop objReader.Close() My.Computer.FileSystem.DeleteFile("c:\Scanning files\lsprint_MO.txt") My.Computer.FileSystem.RenameFile(Temp_file, "c:\Scanning files\lsprint_MO.txt") End If End Sub Private Function WriteFile(ByVal Output_File As String, ByVal fname As String) As Boolean Dim oWrite As New System.IO.StreamWriter(Output_File) oWrite = IO.File.AppendText(fname) Thanks! jds1207

        M 1 Reply Last reply
        0
        • J jds1207

          I got this error message: Illegal characters in path. This line was highlighted: Dim oWrite As New System.IO.StreamWriter(Output_File) Also, am I on the right track? Private Sub CleanFile() 'Read file line by line Dim file_name As String = "c:\Scanning files\lsprint_MO.txt" Dim textline As String Dim Temp_file As String = My.Computer.FileSystem.GetTempFileName If System.IO.File.Exists(file_name) = True Then Dim objReader As New System.IO.StreamReader(file_name) Do While objReader.Peek() - 1 textline = objReader.ReadLine() & vbNewLine If textline.IndexOf("xxxxxxx") = 17 Then 'Console.WriteLine(textline) WriteFile(textline, "c:\Scanning files\lsprint_MO.txt") End If Loop objReader.Close() My.Computer.FileSystem.DeleteFile("c:\Scanning files\lsprint_MO.txt") My.Computer.FileSystem.RenameFile(Temp_file, "c:\Scanning files\lsprint_MO.txt") End If End Sub Private Function WriteFile(ByVal Output_File As String, ByVal fname As String) As Boolean Dim oWrite As New System.IO.StreamWriter(Output_File) oWrite = IO.File.AppendText(fname) Thanks! jds1207

          M Offline
          M Offline
          Martin Smith
          wrote on last edited by
          #4

          I notice you have got the parameters the wrong way round in your call to the WriteFile() function.... You are basically on the right track: Here is my (slightly corrected) version of your WriteFile() function: Private Sub WriteFile(ByVal Output_file as string, ByVal The_text as String) Dim oWrite As System.IO.StreamWriter If File.Exists(Output_file) = False Then ' Create a new file to write to. oWrite = IO.File.CreateText(Output_file) Else ' Append to file oWrite = IO.File.AppendText(Output_file) End If oWrite.WriteLine(The_text) oWrite.Flush() oWrite.Close() End Sub And here is my version of your CleanFile() Function: Private Sub CleanFile() 'Read file line by line Dim file_name As String = "c:\Scanning files\lsprint_MO.txt" Dim Temp_file As String = My.Computer.FileSystem.GetTempFileName Dim textline As String If System.IO.File.Exists(file_name) = True Then Dim objReader As New System.IO.StreamReader(file_name) Do While objReader.Peek() - 1 textline = objReader.ReadLine() & vbNewLine If textline.IndexOf("xxxxxxx") = 17 Then 'Console.WriteLine(textline) WriteFile(Temp_file, textline) End If Loop objReader.Close() My.Computer.FileSystem.DeleteFile("c:\Scanning files\lsprint_MO.txt") My.Computer.FileSystem.RenameFile(Temp_file, "c:\Scanning files\lsprint_MO.txt") End If End Sub One thing to note here, your original lsprint_MO.txt file will be overwritten by the process of running CleanFile().... I hope this clears everything up for you.... Have fun and best regards, Martin

          J 1 Reply Last reply
          0
          • M Martin Smith

            I notice you have got the parameters the wrong way round in your call to the WriteFile() function.... You are basically on the right track: Here is my (slightly corrected) version of your WriteFile() function: Private Sub WriteFile(ByVal Output_file as string, ByVal The_text as String) Dim oWrite As System.IO.StreamWriter If File.Exists(Output_file) = False Then ' Create a new file to write to. oWrite = IO.File.CreateText(Output_file) Else ' Append to file oWrite = IO.File.AppendText(Output_file) End If oWrite.WriteLine(The_text) oWrite.Flush() oWrite.Close() End Sub And here is my version of your CleanFile() Function: Private Sub CleanFile() 'Read file line by line Dim file_name As String = "c:\Scanning files\lsprint_MO.txt" Dim Temp_file As String = My.Computer.FileSystem.GetTempFileName Dim textline As String If System.IO.File.Exists(file_name) = True Then Dim objReader As New System.IO.StreamReader(file_name) Do While objReader.Peek() - 1 textline = objReader.ReadLine() & vbNewLine If textline.IndexOf("xxxxxxx") = 17 Then 'Console.WriteLine(textline) WriteFile(Temp_file, textline) End If Loop objReader.Close() My.Computer.FileSystem.DeleteFile("c:\Scanning files\lsprint_MO.txt") My.Computer.FileSystem.RenameFile(Temp_file, "c:\Scanning files\lsprint_MO.txt") End If End Sub One thing to note here, your original lsprint_MO.txt file will be overwritten by the process of running CleanFile().... I hope this clears everything up for you.... Have fun and best regards, Martin

            J Offline
            J Offline
            jds1207
            wrote on last edited by
            #5

            I have the program running but it has been running for quiet a while now. Does it suppose to take this long to complete? jds1207

            M 1 Reply Last reply
            0
            • J jds1207

              I have the program running but it has been running for quiet a while now. Does it suppose to take this long to complete? jds1207

              M Offline
              M Offline
              Martin Smith
              wrote on last edited by
              #6

              I guess it will all depend on the size of the input file.... Perhaps a tweak to the "while" loop inside your cleanFile() function may help speed things up: Private Sub CleanFile() 'Read file line by line Dim file_name As String = "c:\Scanning files\lsprint_MO.txt" Dim Temp_file As String = My.Computer.FileSystem.GetTempFileName Dim textline As String If System.IO.File.Exists(file_name) = True Then Dim objReader As New System.IO.StreamReader(file_name) Do textline = String.Concat(objReader.ReadLine(), ControlChars.NewLine) If textline.IndexOf("xxxxxxx") = 17 Then WriteFile(Temp_file, textline) Loop Until textline Is Nothing objReader.Close() My.Computer.FileSystem.DeleteFile("c:\Scanning files\lsprint_MO.txt") My.Computer.FileSystem.RenameFile(Temp_file, "c:\Scanning files\lsprint_MO.txt") End If End Sub Other than that, I'm affraid I have no ideas..... Regards, Martin

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups