tags:

views:

41

answers:

2

Using VB6

code

Dim fso As FileSystemObject
Dim TS As TextStream
Dim TempS As String
Dim Final As String
Set fso = New FileSystemObject
Set TS = fso.OpenTextFile(1.txt, ForReading)
Final = TS.ReadAll
Do Until TS.AtEndOfStream
    TempS = TS.ReadLine
    Final = Final & TempS & vbCrLf
Loop
TS.Close
Set TS = fso.OpenTextFile(App.Path & "\Staff.txt", ForAppending, True)
    TS.Write Final
TS.Close
Set TS = Nothing
Set fso = Nothing

Above code is working, but I want add one more line while writing in Staff.txt.

Textfile

1.txt

M3,4331,57,0,3,,20090405,153601,8193,3,0,,,,
M3,4440,59,0,3,,20090405,172110,8193,3,0,,,,
M3,4439,66,0,1,,20090405,172106,8193,3,0,,,,
M3,4374,68,0,1,,20090405,165003,8193,3,0,,,,

Expected Output

When writing a file as Staff.txt

Col1, col2, col3, col4, col5, col6..
M3,4331,57,0,3,,20090405,153601,8193,3,0,,,,
M3,4440,59,0,3,,20090405,172110,8193,3,0,,,,
M3,4439,66,0,1,,20090405,172106,8193,3,0,,,,
M3,4374,68,0,1,,20090405,165003,8193,3,0,,,,

I want to add above one line like col1, col2, col3…. So on…,. How to modify a code?

Need VB6 Code Help

+1  A: 

First you should have a string variable to write columns names to:

Dim header as String
Dim i as Integer
For i=0 To NumberOfColumns-1
  header=header &"Col" & i & ","
Next i

Then you should write the header to your TextStream before writing any other text:(before TS.Write Final)

TS.Write Header & vbCrLf
TS.Write Final
Beatles1692
A: 

try

Set TS = fso.OpenTextFile(App.Path & "\Staff.txt", ForAppending, True)
    TS.WriteLine("col1, col2, col3, col4, col5, col6")
    TS.Write Final
TS.Close

Also, I douth this code is working. For one, you have fso.OpenTextFile(1.txt, ForReading), should be "1.txt".

Kobi