• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 136
  • Last Modified:

Appending records in a fixed length text file

I have a fixed length text file named AMCK_GL2 with two columns of data e.g.

3.00      1492.00        
3.00      975.00        
3.00      959.00        
3.00      1150.00        
3.00      1175.00        
3.00      771.00        
3.00      708.00        
3.00      1323.00        
3.00      538.00        
3.00      993.00

What I want to do is capture the file name (AMCK_GL2) in a variable and append each record in the text file so the final product is as follows;

AMCK_GL2      3.00      1492.00        
AMCK_GL2      3.00      975.00        
AMCK_GL2      3.00      959.00        
AMCK_GL2      3.00      1150.00        
AMCK_GL2      3.00      1175.00        
AMCK_GL2      3.00      771.00        
AMCK_GL2      3.00      708.00        
AMCK_GL2      3.00      1323.00        
AMCK_GL2      3.00      538.00        
AMCK_GL2      3.00      993.00

There is probably a very simple answer to the challenge.  For me, however, the challenge is not simple.  I need to solve this problem as soon as possible. Any assistance will be appreciated.        
1 Solution
Private Sub Command1_Click()
    Set fso = CreateObject("Scripting.FileSystemObject")
    Dim ReadFile, WriteFile, sTextLine
    Set ReadFile = fso.OpenTextFile("c:\AMCK_GL2.txt", ForReading)
    Set WriteFile = fso.OpenTextFile("c:\AMCK_GL2.~txt", ForWriting, True)
    With ReadFile
        While Not .AtEndOfStream
            WriteFile.WriteLine "AMCK_GL2      " & RTrim(.ReadLine)
    End With
    With fso
        .CopyFile "c:\AMCK_GL2.~txt", "c:\AMCK_GL2.txt", True
        .DeleteFile "c:\AMCK_GL2.~txt", True
    End With    
End Sub
Try this:

Private Sub Command1_Click()
    Dim i, j As Integer
    Dim LineText, NewText, Filename As String
    Dim SourceFile, TempFile As String
    ' Source text file
    SourceFile = "C:\My_Space\Test.txt"
    ' Temporary file
    TempFile = "C:\My_Space\TempTest.txt"
    ' The extract filename
    Filename = "AMCK_GL2"
    ' Opening the source file to read
    i = FreeFile
    Open SourceFile For Input As i
    ' Opening the temporary file to write
    j = FreeFile
    Open TempFile For Output As j
    ' Loop until the end of file not found in the source file
    Do While Not EOF(i)
        ' Read a line
        Line Input #i, LineText
        ' Prepare the text of line for the temporary file
        NewText = Filename & Space(15 - Len(Filename)) & LineText
        ' Insert the new line in temporary file
        Print #j, NewText
    ' Closing both original and temporary file
    Close #i
    Close #j
    ' Copy the temporary file to the original file
    Call FileCopy(TempFile, SourceFile)
    ' Delete the temporary file
    Call Kill(TempFile)
End Sub

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now