Append text file
-
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
-
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
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
-
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
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
-
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
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 -
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 -
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
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