Results 1 to 5 of 5

Thread: Replacing text in a file based on position

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2007
    Posts
    3

    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.
    Last edited by cferdig; May 10th, 2007 at 06:05 AM. Reason: Added VB version

  2. #2
    Frenzied Member
    Join Date
    Jun 2005
    Posts
    1,950

    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.
    Last edited by Bulldog; May 10th, 2007 at 07:50 AM.

  3. #3

    Thread Starter
    New Member
    Join Date
    May 2007
    Posts
    3

    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.

  4. #4
    Frenzied Member stimbo's Avatar
    Join Date
    Jun 2006
    Location
    UK
    Posts
    1,739

    Re: Replacing text in a file based on position

    Like this?
    vb Code:
    1. Dim str() As String = IO.File.ReadAllLines("replace.txt")  'Path name
    2. Dim temp As String = Nothing
    3.      
    4.      For i As Integer = 0 To str.Length - 1
    5.          temp = str(i).Substring(22)
    6.          str(i) = str(i).Remove(0, 8)
    7.          str(i) = str(i).Insert(0, temp)
    8.      Next
    9.  
    10. IO.File.WriteAllLines("Replace2.txt", str)    'New file to test
    Stim

    Free VB.NET Book Chapter
    Visual Basic 2005 Cookbook Sample Chapter

  5. #5

    Thread Starter
    New Member
    Join Date
    May 2007
    Posts
    3

    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

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