Making one textfile by coping another two textfiles

A

Alogon

I have an "A" textfile and a "B" textfile. So I want to create by VBA a "C"
textfile which contents are data from "A" and "B" textfiles.

I can create the "C" textfile and copy there data from "A" textfile, but
when I try to copy data from "B" textfile, I can not retain the data that was
copied from "A" textfile.

Any soul could help me?
 
G

Gary''s Student

Assuming the files are in C:\Temp

Put the following lines in a file called something.bat:

C:
cd\Temp
type A.txt > C.txt
type B.txt >> C.txt

then double-click the .bat file
 
G

GeoffG

Copy the code below to a blank new module
for a solution in VBA. Set the Reference
as instructed below.


' This module requires a reference to the
' Microsoft Scripting Runtime Object Library.
' To set the reference, follow these steps:
' 1. In the VBA editor, open the TOOLS menu
' and select REFERENCES.
' 2. In the References dialog, select
' Microsoft Scripting Runtime.
' 3. Click the OK button to close the
' References dialog.

Private Sub CopyTextFiles()

Const strcPath As String = "C:\My Documents\"

Dim objFSO As Scripting.FileSystemObject
Dim objTSA As Scripting.TextStream
Dim objTSB As Scripting.TextStream
Dim objTSC As Scripting.TextStream

Dim strA As String
Dim strB As String

' Create an object variable pointing to
' a FileSystemObject:
Set objFSO = New Scripting.FileSystemObject

' Create object variables pointing to the
' two existing text files, A and B:
Set objTSA = objFSO.OpenTextFile(strcPath & "A.txt")
Set objTSB = objFSO.OpenTextFile(strcPath & "B.txt")

' Create an object variable pointing to
' a new text file, C, overwriting any existing file:
Set objTSC = objFSO.CreateTextFile(strcPath & "C.txt", True)

' Read files A and B:
strA = objTSA.ReadAll
strB = objTSB.ReadAll

' Write strA to file C:
objTSC.Write strA

' Add two blank lines to file C:
objTSC.WriteBlankLines 2

' Write strB to file C:
objTSC.Write strB

' Close the files:
objTSA.Close
objTSB.Close
objTSC.Close

' Destroy object variables:
Set objTSA = Nothing
Set objTSB = Nothing
Set objTSC = Nothing
Set objFSO = Nothing

End Sub

Regards
Geoff
 
K

Karl E. Peterson

Alogon said:
I have an "A" textfile and a "B" textfile. So I want to create by VBA a "C"
textfile which contents are data from "A" and "B" textfiles.

I can create the "C" textfile and copy there data from "A" textfile, but
when I try to copy data from "B" textfile, I can not retain the data that was
copied from "A" textfile.

Any soul could help me?

Call WriteFile("C", ReadFile("A") & ReadFile("B"))

Public Function ReadFile(ByVal FileName As String) As String
Dim hFile As Long
On Error GoTo Hell
hFile = FreeFile
Open FileName For Binary As #hFile
ReadFile = Space$(LOF(hFile))
Get #hFile, , ReadFile
Close #hFile
Hell:
End Function

Public Function WriteFile(ByVal FileName As String, ByVal Text As String) As
Boolean
Dim hFile As Long
On Error GoTo Hell
hFile = FreeFile
Open FileName For Output As #hFile
Print #hFile, Text;
Close #hFile
Hell:
WriteFile = Not CBool(Err.Number)
End Function
 

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