Solved

Appending records in a fixed length text file

Posted on 2004-09-04
2
124 Views
Last Modified: 2010-05-18
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.        
0
Comment
Question by:indywebteam
2 Comments
 
LVL 18

Expert Comment

by:JR2003
ID: 11980144
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)
        Wend
        .Close
    End With
    WriteFile.Close
    With fso
        .CopyFile "c:\AMCK_GL2.~txt", "c:\AMCK_GL2.txt", True
        .DeleteFile "c:\AMCK_GL2.~txt", True
    End With    
End Sub
0
 
LVL 5

Accepted Solution

by:
KarcOrigin earned 250 total points
ID: 11986895
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
    Loop
   
    ' 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

Cheers!
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now