Appending Txt files coding

N

Nadine

I can open a file but the file is not passing through with the code. Do I
need to write file and not line as follows:
Private Sub CreateAfile_Click()
Set fs = CreateObject("Scripting.FileSystemObject")
Set a = fs.CreateTextFile("c:\testfile.txt", True)
a.Writeline ("C:\Direct Debits\strFile2.txt")
a.Close
End Sub
 
S

Surendran

Hi Nadine,

I happen to see your posting and observe the following.

You have not declared the variables of proper type.
Set a = fs.CreateTextFile("c:\testfile.txt", True)
For instance in the above line, CreateTextFile opens the file and return a
textStream object, so variable 'a' is to be declared as text stream object.
a.Writeline ("C:\Direct Debits\strFile2.txt")
In the 'WriteLine' method, you have to give a text line usually by reading
from the source file.

Anyway I give below a complete code for appending text files to another text
file.This code can be copied to any standard module, change path of source
and output files as appropriate and may be tested in the immediate window.

'================== Start Code ===========================
Option Compare Database
Option Explicit

'The following procedure "TextFileAppend" will read text files from a
designated
'directory and append to a new text file or existing file in the directory
path specified.
'In the code, the path for the files to be read (strFromDir)
'the file to be appended and the path (strToDir) for the append file
'is only given as an example to follow the code. Users may change the path
and file
' names as appropriate according to the path and file names set in their
machine.
'If wrong directory path is given the code will generate error.
'If the file to be appended already exists in the file path(strToDir), the
code will
'simply open the file.
'Otherwise a new file will be created for appending.
'Ensure that reference is set to "Microsoft Scripting Runtime" under
tools/references.
'For testing purpose keep few text files ( 3 or 4) in the path strFromDir

Public Sub TextFileAppend()
On Error GoTo Err_TextFileAppend
Dim fso As New FileSystemObject
Dim ts As TextStream, tsNew As TextStream

Dim strLine As String

Dim strFileFrom As String, strFileTo As String
Dim strFromDir As String, strToDir As String

Dim f As File
Dim fromFol As Folder, toFol As Folder

'Change the directory path as appropriate
strFromDir = "C:\Files\FilesFromDir"
strToDir = "C:\Files\FilesToDir"

Set fromFol = fso.GetFolder(strFromDir)
Set toFol = fso.GetFolder(strToDir)

' Open the text file to append if already exists else create a new file in
the path
' (strToDir) and open for appending.
Set tsNew = fso_OpenTextFile(strToDir & "\" & "FileAppend.TXT",
ForAppending, True)

'Open files to be read one by one, read and append to the FileAppend.TXT.
For Each f In fromFol.Files
strFileFrom = strFromDir & "\" & f.Name 'Full path of the File to
be read

'Open the file for reading
Set ts = fso_OpenTextFile(strFileFrom)

'Loop while not at the end of the file. i.e. read line by line and write(or
append) to
'the FileAppend.txt
Do While Not ts.AtEndOfStream
strLine = ts.ReadLine

'This is the place to process the line if required. i.e. to remove
extraneous spaces,
' read selected text, skip lines, or if data is delimited by tab, etc. you
can even
'extract select data to be inserted in an Access Table by using a
combination of
'String handling functions and recordset objects.
'Use a counter variable to know the line number you are reading now.

'Append the line to FileAppend.TXT
tsNew.WriteLine strLine

Loop

'Now one file is read and written in FileAppend.TXT. If blank line is to be
'inserted between files appended then insert the following code. otherwise
'comment it.
tsNew.WriteBlankLines (1)

'Close the text stream object so that it can open other files if any afresh
for reading
' in the next cycle.
ts.Close

Next

'Close the append file text stream object.
tsNew.Close

Exit_TextFileAppend:
Exit Sub

Err_TextFileAppend:
MsgBox "Error Number: " & Err.Number & " - " & Err.Description
Resume Exit_TextFileAppend

End Sub
================== End Code ============================

Good Luck,
Surendran
 

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