Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Appending Text Files

1 view
Skip to first unread message

Ken Halter

unread,
Feb 9, 2004, 12:26:17 PM2/9/04
to
This should work... just keep feeding files to the AppendFile method
'===============
Option Explicit

Private Sub Command1_Click()
Dim sTargetFile As String
Dim sSourceFile As String

sTargetFile = "C:\Temp\Test.txt"

sSourceFile = "C:\Temp\Test1.txt"
Call AppendFile(sTargetFile, sSourceFile)

sSourceFile = "C:\Temp\Test2.txt"
Call AppendFile(sTargetFile, sSourceFile)


End Sub

Private Sub AppendFile(TargetFile As String, SourceFile As String)
Dim iTargetFile As Integer
Dim iSourceFile As Integer
Dim sBuffer As String
Dim lLen As Long
Dim lLoop As Long
Dim lCutPoint As Long

iSourceFile = FreeFile
Open SourceFile For Input As iSourceFile
sBuffer = Input$(LOF(iSourceFile), iSourceFile)
Close iSourceFile
lLen = Len(sBuffer)

lCutPoint = lLen
For lLoop = lLen To 1 Step -1
If Asc(Mid$(sBuffer, lLoop, 1)) > 31 Then
lCutPoint = lLoop
Exit For
End If
Next

iTargetFile = FreeFile
Open TargetFile For Append As iTargetFile
Print #iTargetFile, Left$(sBuffer, lCutPoint) & vbCrLf
Close #iTargetFile

End Sub
'===============

--
Ken Halter - MS-MVP-VB - http://www.vbsight.com
Please keep it in the groups..


"jack" <jgp...@wi.rr.com> wrote in message
news:oKOVb.32158$2h....@twister.rdc-kc.rr.com...
> Hi;
>
> I am having some trouble in getting to append anywhere from 2 to 50 text
> files. These appended files will be imported into an Excel 2002
> Workbook/Worksheet.
>
> I tried a couple of programs on several sites, but none work or are
> scripting files, which I would rather not use. On the code I did, I keep
> getting a String out of space error. Or - Bad file name or Open already.
> It's been a couple of years since I did any serious programm--I'm kinda
> rusty
>
> Is there anybody who has done this that can advise me
>
> TIA -- Jack
> P.S. I got the opening of a Spreadsheet to work fine and am able to get the
> info into the cells.
>
>


Rick Rothstein

unread,
Feb 9, 2004, 1:38:53 PM2/9/04
to
Wouldn't this shorter approach do the same thing as yours?

Private Sub AppendFile(TargetFile As String, SourceFile As String)

Dim TotalFile As String
Dim FileNum As Integer
FileNum = FreeFile
Open SourceFile For Binary As #FileNum
TotalFile = Space(LOF(FileNum))
Get #FileNum, , TotalFile
Close #FileNum
FileNum = FreeFile
Open TargetFile For Append As #FileNum
Print TotalFile & vbNewLine
Close #FileNum
End Sub

Rick - MVP

Jeff Johnson [MVP: VB]

unread,
Feb 9, 2004, 1:58:40 PM2/9/04
to
Fix your system clock. Future-posting is rude.


jack

unread,
Feb 10, 2004, 9:45:15 AM2/10/04
to
This works great for the 2 files you declare, but if I connect it to a
commondialog box I keep getting out of string space errors when I
select the 3rd file and the program stops.

If I use the second method I get a out of memory error. I have 2Gbytes
of memory on my system(Thats Gigabytes!) and find it hard to believe
this error.

What do I do next.

Please advise.

Jack

"Rick Rothstein" <rickNOS...@NOSPAMcomcast.net> wrote in message
news:%23DI01vz...@tk2msftngp13.phx.gbl...

jack

unread,
Feb 10, 2004, 12:17:13 PM2/10/04
to
Never mind I figured it out myself, just redid the common dialog
code and it works fine!!!!!!!

Thanks alot Guys!!! -- Knew it had to be me

Jack


"jack" <jgp...@wi.rr.com> wrote in message

news:H76Wb.33434$2h.2...@twister.rdc-kc.rr.com...

0 new messages