writeline and transfertext

B

bill

Hi All, sorry for the long winded note...

I've created the following text file in two steps 1)
transfertext and 2)writeline, both vb methods.
TransferText creates line 03 and writeline does 01/02. As
you can see the 01, 02, 03, prefixes are out of order
(03,01,02).

------------------------------------------------
03 B00000656 3.09 -72
01 45675 JAMES GANG SCREENPRINTING 508720
02 6 1 3278 MISPULL 0 -19 0 0
------------------------------------------------

The resulting file is in this order b/c:

When the TransferText (step 1) command writes to a file,
it overwrites any existing text in the file, so it has to
be step 1...

The Writeline method (step 2) appends text to a file and
doesn't overwrite, but it appends text to the end of the
file. Thus it opens a file with the existing 03 line and
inserts 01 and 02 at the end resulting in 03, 01, 02.

So my question is/are:

Does anyone know of a way to make TransferText not write
over existing data? -or Is there a way to make Writeline
append the beginning rather than the end of the file? -or
can I take the existing incorrectly ordered file and re
order it (DOS sort will do this but I wanted post this
before I start looking into writing a batch file)

Any suggestions are appreciated.
 
K

Ken Snell [MVP]

Why not create two separate text files and then combine them into a single
text file with the records in the correct order?

Here's some sample code that opens two text files and creates a new one from
the two's contents:

Dim strFileIn As String, strFileOut As String, strLine As String
Dim strNewLine As String
Dim intCount As Integer

' Path and file of text file being read/converted
strFileIn = "C:\MyFolder\TextFileName.txt"
' Path and file of text file being created
strFileOut = "C:\MyFolder\NewTextFileName.txt"

Open strFileIn For Input As #1
Open strFileOut For Output As #2

intCount = 0
strNewLine = ""

Do While EOF(1) = False
Line Input #1, strLine
intCount = intCount + 1
' Combine the line with the other lines of a group,
' using pipe character "|" as the delimiter
strNewLine = strNewLine & strLine & "|"
' If this is the third line of a group, write it out
' to the new file and reset the counter
If intCount = 3 Then
Print #2, strNewLine
strNewLine = ""
End If
Loop

Close #1
Close #2
 
D

Dirk Goldgar

bill said:
Hi All, sorry for the long winded note...

I've created the following text file in two steps 1)
transfertext and 2)writeline, both vb methods.
TransferText creates line 03 and writeline does 01/02. As
you can see the 01, 02, 03, prefixes are out of order
(03,01,02).

------------------------------------------------
03 B00000656 3.09 -72
01 45675 JAMES GANG SCREENPRINTING 508720
02 6 1 3278 MISPULL 0 -19 0 0
------------------------------------------------

The resulting file is in this order b/c:

When the TransferText (step 1) command writes to a file,
it overwrites any existing text in the file, so it has to
be step 1...

The Writeline method (step 2) appends text to a file and
doesn't overwrite, but it appends text to the end of the
file. Thus it opens a file with the existing 03 line and
inserts 01 and 02 at the end resulting in 03, 01, 02.

So my question is/are:

Does anyone know of a way to make TransferText not write
over existing data? -or Is there a way to make Writeline
append the beginning rather than the end of the file? -or
can I take the existing incorrectly ordered file and re
order it (DOS sort will do this but I wanted post this
before I start looking into writing a batch file)

Any suggestions are appreciated.

Are you writing just these three lines into the file? I ask because, if
you need to create multiple sets of 01, 02, 03, 01, 02, 03 ..., it's
going to limit your options.

As far as I know, there's no way to make TransferText append to an
existing file, nor is there any way to write to the beginning of a text
file without overwriting what's already there. You could, of course,
write two separate files and then combine them. But your best approach
is probably not to use Transfertext at all.

Instead, you can open a recordet on the query or table that you want to
save to text, and use ordinary text I/O or the TextStream.WriteLine
method to write all the lines -- 01, 02, *and* 03 -- assembling the
third line from the fields in the current record of the recordset.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top