Replacing text in a file based on position
What I am trying to accomplish is this in VB 2005:
Here is a sample of input data from a text file-
000000001111111122222233333333
000000001111111122222244444444
000000001111111122222255555555
What I would like to do is read 8 characters starting at position 23 and overwrite the first 8 characters in the line. The output would look like this:
333333331111111122222233333333
444444441111111122222244444444
555555551111111122222255555555
The characters are varaiable but the chunks I need to read and write are always in the same positions.
There are possibly several thousand lines and up to 10 seperate files in a folder that I need to process this way. I am new to VB 2005 and having a difficult time. Any help would be greatly appreciated.
Re: Replacing text in a file based on position
This should do it. This can handle very big files.
Code:
Option Strict On
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ChangeFile("Input file name", "The output file name")
End Sub
Private Sub ChangeFile(ByVal inputfilename As String, ByVal outputfilename As String)
Dim line As String = String.Empty
Dim r As New System.IO.StreamReader(inputfilename)
Dim w As New System.IO.StreamWriter(outputfilename, False)
While r.Peek <> -1
line = r.ReadLine
w.WriteLine(line.Substring(22, line.Length - 22) & line.Substring(9, line.Length - 9))
End While
r.Close()
w.Close()
End Sub
End Class
If your filenames are all very similar you could also make an array of the filenames and do a For Next loop to process each one. If they are all different names then you could do this...
Code:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
For Each file As String In System.IO.Directory.GetFiles("YourFolderPath", "*.*", IO.SearchOption.TopDirectoryOnly)
ChangeFile(file, file & ".out")
Next
End Sub
The "*.*" is the search criteria, so this would do all of the files, this can be made more specific like "*.txt" or whatever. You can also use IO.SearchOption.AllDirectories to include any sub folders within the folder at that path.
Re: Replacing text in a file based on position
Close but not what I was looking for. This script only removed the first 22 characters from each row. The file layout vcannot change. I need to copy characters in positions 22-29 and then replace the contents of characters in positions 0-7.
Re: Replacing text in a file based on position
Like this?
vb Code:
Dim str() As String = IO.File.ReadAllLines("replace.txt") 'Path name
Dim temp As String = Nothing
For i As Integer = 0 To str.Length - 1
temp = str(i).Substring(22)
str(i) = str(i).Remove(0, 8)
str(i) = str(i).Insert(0, temp)
Next
IO.File.WriteAllLines("Replace2.txt", str) 'New file to test
Re: Replacing text in a file based on position
Here is the updated code that I was looking for. Thanks so much. I have been using VB 6 for 5 years and just cannot get my head around 2005 version. It'll come in time.
Private Sub ChangeFile(ByVal inputfilename As String, ByVal outputfilename As String)
Dim line As String = String.Empty
Dim r As New System.IO.StreamReader(inputfilename)
Dim w As New System.IO.StreamWriter(outputfilename, False)
While r.Peek <> -1
line = r.ReadLine
w.WriteLine(line.Substring(17, 8) & line.Substring(8))
End While
r.Close()
w.Close()
End Sub