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
"Nadine" wrote:
> Thanks for that I will try and let you know.
>
> "Klatuu" wrote:
>
> > That's not going to work. I was thinking in terms of opening a file, writing
> > the first 80 characters to that file, then the next 80, then the 120 and
> > closing the file.
> >
> > "Nadine" wrote:
> >
> > > thanks the code is as follows I hope it makes sense.
> > >
> > > Option Compare Database
> > > strFileOut = "C:\Direct Debits\Bankfile\master.txt"
> > > Shell " COPY """ & strFile1 & """ + """ & strFile2 & """ """ & strFileOut &
> > > """", vbMinimizedNoFocus
> > >
> > > "Klatuu" wrote:
> > >
> > > > post the code you wrote, I will look at it.
> > > >
> > > > "Nadine" wrote:
> > > >
> > > > > I have but it didn't wrong . Can you suggest.
> > > > >
> > > > > Thanks
> > > > >
> > > > > NAdine
> > > > >
> > > > > "Klatuu" wrote:
> > > > >
> > > > > > Then I would use VBA to write directly to the text file.
> > > > > >
> > > > > > "Nadine" wrote:
> > > > > >
> > > > > > > Thank you for the reply. I have tried this but the problem is the
> > > > > > > application that requires this file needs all the spaces removed ie the first
> > > > > > > line in the file must be 80 Characters, the second line 80 Characters and the
> > > > > > > third line 120 characters and the last line again is 80 characters. Hope this
> > > > > > > gives you more of an insight as to what I need.
> > > > > > >
> > > > > > > Thanks
> > > > > > >
> > > > > > > Nadine
> > > > > > >
> > > > > > > "Klatuu" wrote:
> > > > > > >
> > > > > > > > When you say different widths, I assume you mean different numbers of fields.
> > > > > > > > I also assume you will know which fields belong in which positions for each
> > > > > > > > of the files you want to export.
> > > > > > > > If my assumptions are correct, I would suggest you create a "transfer" table
> > > > > > > > that is set up to be the widest possible with all the necessary fields and
> > > > > > > > data types. Then, create an append query for each of the tables you want to
> > > > > > > > export that will append the data to the transfer table. The use the transfer
> > > > > > > > table in the TransferText method.
> > > > > > > >
> > > > > > > > "Nadine" wrote:
> > > > > > > >
> > > > > > > > > Hi,
> > > > > > > > > I am trying to export out multiple files with different width settings to
> > > > > > > > > one text file.
> > > > > > > > > I am looking for the code to do this within access. Some one told me to
> > > > > > > > > export the files and then shell to the windows copy command to concatenate
> > > > > > > > > them , but I can not get this to work.
> > > > > > > > >
> > > > > > > > > Thanks
> > > > > > > > >
> > > > > > > > > Nadine
Surendran - 06 Jul 2006 13:11 GMT
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
> 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:
[quoted text clipped - 75 lines]
> > > > > > > > > >
> > > > > > > > > > Nadine