Append text file

J

Jeff

Hi,

I have two text files and I want to append TextFileB onto the end of TextFileA

TextFileA is 3 million rows
TextFileB is 100 rows

The problem is that TextFileA is 3 million rows long. So I do not want to
create an array, load all of TextFileA, then output TextFileA and then the
100 lines of TextFileB (that would take a long time). That is the only way I
know of to append B to the end of A.

Can anyone help?

Thanks!
 
J

JW

How about something like this.
fNum=FreeFile
Open TextFileA For Append As fNum
Print fNum, TextFileB
Close fNum
 
J

Jeff

Hi,

I tried the following code but go the error message "method not valid
without suitable object" on the line 'Print fNum, TextFileB'

Code:
Sub TestCode()

'How about something like this.
'fNum = FreeFile
'Open TextFileA For Append As fNum
'Print fNum, TextFileB
'Close fNum

TextFileA = "C:\IScenarioSets1.csv"
TextFileB = "C:\IScenarioSets2.csv"

fNum = FreeFile
Open TextFileA For Append As fNum
Print fNum, TextFileB
Close fNum

End Sub
 
J

JW

Give this a go.
Sub TestCode()
Dim fNum As Integer, fNum2 As Integer
Dim yourInfo As String
TextFileA = "C:\IScenarioSets1.csv"
TextFileB = "C:\IScenarioSets2.csv"
fNum = FreeFile
Open TextFileA For Append As fNum
fNum2 = FreeFile
Open TextFileB For Input As fNum2
Do While Not EOF(fNum2)
Line Input #fNum2, yourInfo
Print #fNum, yourInfo
Loop
Close #fNum
Close #fNum2
End Sub
 
R

Rick Rothstein \(MVP - VB\)

If your files are really **text** files as the names indicate, you can
always go "lo-tech"... start Windows command processor (Start
Button/(All)Programs/Accessories/CommandPrompt)... if your files are both in
the same directory, 'cd' to that directory, for example....

cd c:\directory1\directory2\etc.

Then execute this command (note the plus sign)...

copy TextFileA.txt+TextFileB.txt TextFileA.txt

If the files are located in different directories, then you can execute the
following command without using 'cd' first...

copy <Path>\TextFileA.txt+<Path>\TextFileB.txt <Path>\TextFileA.txt

where you would substitute the complete path to each file where I have
indicated <Path>. By the way, if you want to test this out before actually
changing TextFileA, change the last file to a non-existent filename and the
concatenated file will be written to it instead. For example, using the
first command where you used 'cd' to move to the common directory...

copy TextFileA.txt+TextFileB.txt TestFile.txt

Doing this will create a new file in the common directory and then place the
concatenated file into it. You can then see if the result is what you want
before making it permanent.

Rick
 
J

Jeff

Thank you both for your help on this!

Rick Rothstein (MVP - VB) said:
If your files are really **text** files as the names indicate, you can
always go "lo-tech"... start Windows command processor (Start
Button/(All)Programs/Accessories/CommandPrompt)... if your files are both in
the same directory, 'cd' to that directory, for example....

cd c:\directory1\directory2\etc.

Then execute this command (note the plus sign)...

copy TextFileA.txt+TextFileB.txt TextFileA.txt

If the files are located in different directories, then you can execute the
following command without using 'cd' first...

copy <Path>\TextFileA.txt+<Path>\TextFileB.txt <Path>\TextFileA.txt

where you would substitute the complete path to each file where I have
indicated <Path>. By the way, if you want to test this out before actually
changing TextFileA, change the last file to a non-existent filename and the
concatenated file will be written to it instead. For example, using the
first command where you used 'cd' to move to the common directory...

copy TextFileA.txt+TextFileB.txt TestFile.txt

Doing this will create a new file in the common directory and then place the
concatenated file into it. You can then see if the result is what you want
before making it permanent.

Rick
 
S

Steve Yandl

Here is one option:

___________________________________

Sub AppendTextFile()

Const ForReading = 1
Const ForAppending = 8

Dim StrTransfer As String

txtFileAname = "C:\IScenarioSet1.csv"
txtFileBname = "C:\IScenarioSet2.csv"

Set FSO = CreateObject("Scripting.FileSystemObject")

Set txtFileB = FSO.OpenTextFile(txtFileBname, ForReading)
StrTransfer = txtFileB.ReadAll
txtFileB.Close

Set txtFileA = FSO.OpenTextFile(txtFileAname, ForAppending)
txtFileA.Write vbCrLf & StrTransfer
txtFileA.Close

Set FSO = Nothing

End Sub

___________________________________

Steve
 
Top