View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.programming
GS[_6_] GS[_6_] is offline
external usenet poster
 
Posts: 1,182
Default Merge several fixed width text files into one

El martes, 5 de febrero de 2008, 15:48:20 (UTC-5), Jim Rech escribió:
Adapt this:

Sub a()
Dim SrcFiles, CurrSrc As String
Dim DestFile As String, Counter As Integer
Dim TextLine As String
SrcFiles = Array("c:\File1.txt", "c:\File2.txt")
Open "c:\file3.txt" For Output As #1
For Counter = 0 To UBound(SrcFiles)
Open SrcFiles(Counter) For Input As #2
Do While Not EOF(2)
Line Input #2, TextLine
Print #1, TextLine
Loop
Close #2
Next
Close #1
End Sub


--
Jim
"XP" wrote in message
...
Using Office 2003 and Windows XP; and using using VBA from MS-Excel;

1) There will be several (between 1 and 10) fixed width text files in a
specific folder, each formatted the same way; the files range from a few
kb to 100kb in size.

2) I need a program that will sequentially open each text file and copy
the contents;

3) The contents of each separate text file, along with all formatting,
must be transferred undisturbed into a new fixed width text file, appending
the contents of each text file into a new SINGLE merged text file;

I can already code the part where the list of files is captured and each
may be opened and closed. I need help with the part where the data is
copied from the text file and then written to the new single merge file.
Can someone please post some example code that would help me get started?

Should I use FileSystemObject? Or something else? Suggestions? Help?

Thanks much in advance for your assistance.


Hi Jim

I've used your example and it works creating a new file, but is there a way
to add the text from file1.txt and file2.txt into the file3.txt.

I mean, updating the file 3 every time I run the code and don´t create a new
file.


If this is being done correctly, each new block of data should be 'appended' to
the target file; -all of which gets done in memory using standard VBA file I/O
functions.

So once the 1st target file is created it would thereafter be re-opened to
append each new block of data. The process will create the file if it doesn't
already exist when it 'dumps' the 1st block of data into it.

Tht said, the proper way to do this is to recurse a folder for .txt files (or
whatever file extension you specify; -csv,dat,tsv...) and append them to a
single file. There already exists a free stand-alone utility (non-Excel) called
*TXTcollector* that you can download here...

https://txtcollector.en.softonic.com/

--
Garry

Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion