How to merge 2 txt files

G

Guest

Hi,

Hope anybody can help me.
Created 2 tabels via append query's and export limited the content to two
text files.
Both txt files have differtent content and export specs.
Now I must merge 2 files. Normally now problem to put 2 files below eachother.
I must merge line under line.
Report can contain upto 500 lines per txt file.

Tx,
Dirk
 
J

John Nurick

Hi Dirk,

If you want to create one file that contains the contents of the first
file followed by the contents of the second, just construct a Windows
command like this

COPY "C:\folder\first file.txt" + "C:\folder\Second file.txt"
"C:\folder\combined file.txt"

and then use the Shell() command to execute it.

If you want to interleave the contents, it's usually best to create an
Access query that produces the results you need.
 
G

Guest

Hi John,

Thanks for your help.
Problem is not solved yet. Big point is the complete diff. export specs per
file. These are limited.
File contains;
Headers, no problem
Line1 file 1
Line1 file 2
Line2 file 1
Line2 file 2
etc.
Footer, no problem

File 1 specs:
Field 1, start 1 width 3
Field 2, start 4, width 3
Field 3, start 7, width 40
Field 4, start 47, width 40
Upto field 82 endwidth 1223

File 2 specs:
Field 1, start 1 width 3
Field 2, start 4, width 8
Field 3, start 12, width 12
Field 4, start 24, width 20
Upto field 16 endwidth 229

When i merge the two query's first via a apendquery into one table and
export the table as far as I know I can only use one export spec.
Now we export both files and copy/past all lines together.
Our customer we send the file to is a big international company otherwise I
had ask them to change their input.

BR,
Dirk
 
J

John Nurick

Hi Dirk,

There are several ways to go. Here are a couple:

1) export the two files as now, then run a script (outside Access) to
interleave them. Here's a Perl script that does the job; you can do the
same thing (if less compactly) in VBScript or other languages:

#Perl script to interleave text files line by line
use strict;
die "Usage:\n perl interleave.pl file1 file2 outfile\n"
unless $ARGV[2];
open IN1, $ARGV[0] or die "Sorry, couldn't open $ARGV[0]. ";
open IN2, $ARGV[1] or die "Sorry, couldn't open $ARGV[1]. ";
open OUT, ">$ARGV[2]" or die "Sorry, couldn't create $ARGV[2].";

my ($in1, $in2);
print OUT "$in1$in2" while (($in1 = <IN1>) && ($in2 = <IN2>));
#end of script

2) In Access VBA, open a recordset on each query and a single textfile,
then assemble and write a line from each recordset in turn. Pseudocode:

Dim rst1 As DAO.Recordset
Dim rst2 As DAO.Recordset

Set rst1 = DBEngine(0)(0).OpenRecordset(blah blah)
Set rst2 = DBEngine(0)(0).OpenRecordset(blah blah)
Open "C:\folder\file.txt" For Output As #1

Do Until (rst1.Eof Or rst2.Eof)
With rst1
strLine = Format(.Fields(1), "blah") _
& Format(.Fields(2) "blah" _
....
#assemble a string containing the record you want to write
Print #1, strLine
End With
With rst2
...
End With
rst1.MoveNext
rst2.MoveNext
Loop
Close #1
rst1.Close
rst2.Close
 

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