none
modify text file insert carriage return RRS feed

  • Question

  • Have a fixed width text file; let’s say: C:\FolderA\Test1.txt  The name and path of the file will not change.

    This text file is an export of a database table, each line is the same character count

    The quantity of lines is variable.

    Example:

    abababCddd

    xyzabaC123

    okwow1Czzz

    (I have capitalize the C to highlight that it is always in the 7<sup>th</sup> position)

    What is needed is an insert of a carriage return to split each line into two at the 7<sup>th</sup> position:

    Ababab

    Cddd

    Xyzaba

    C123

    okwow1

    Czzz

    Also it is ok if a new file is made resulting in let’s say: C:\FolderA\Test2.txt if that helps keep things simple.  The code would be triggered from an Access database event.

    Wednesday, February 18, 2015 5:40 PM

Answers

  • Sub SplitLinesOfFile()
        Dim ResultStr As String
        Dim FileNameIn As String
        Dim FileNameOut As String
        Dim FileNumIn As Integer
        Dim FileNumOut As Integer
        
        FileNameIn = "C:\FolderA\Test1.txt"
        FileNameOut = "C:\FolderA\TempFile.txt"
        
        FileNumIn = FreeFile()
        Open FileNameIn For Input As #FileNumIn
        
        FileNumOut = FreeFile()
        Open FileNameOut For Output Access Write As #FileNumOut
        
        Do While Seek(FileNumIn) <= LOF(FileNumIn)
            Line Input #FileNumIn, ResultStr
            Print #FileNumOut, Left(ResultStr, 6)
            Print #FileNumOut, Mid(ResultStr, 7, Len(ResultStr))
        Loop
        
        Close FileNumIn
        Close FileNumOut
        
        Kill FileNameIn
        Name FileNameOut As FileNameIn
        
    End Sub
    Wednesday, February 18, 2015 6:05 PM

All replies

  • Sub SplitLinesOfFile()
        Dim ResultStr As String
        Dim FileNameIn As String
        Dim FileNameOut As String
        Dim FileNumIn As Integer
        Dim FileNumOut As Integer
        
        FileNameIn = "C:\FolderA\Test1.txt"
        FileNameOut = "C:\FolderA\TempFile.txt"
        
        FileNumIn = FreeFile()
        Open FileNameIn For Input As #FileNumIn
        
        FileNumOut = FreeFile()
        Open FileNameOut For Output Access Write As #FileNumOut
        
        Do While Seek(FileNumIn) <= LOF(FileNumIn)
            Line Input #FileNumIn, ResultStr
            Print #FileNumOut, Left(ResultStr, 6)
            Print #FileNumOut, Mid(ResultStr, 7, Len(ResultStr))
        Loop
        
        Close FileNumIn
        Close FileNumOut
        
        Kill FileNameIn
        Name FileNameOut As FileNameIn
        
    End Sub
    Wednesday, February 18, 2015 6:05 PM
  • Worked great - much thanks
    Wednesday, February 18, 2015 8:27 PM