Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27
Default Appending a file

Hello,
I have the following code to combine the contents of all the text files in
one folder, into one single text file.

The problem is, I get an error 70 Permisson denied on the second time I try
to open the CombindedFile. I don't beleive the file is actually being
opened, but do I need to close it or deactivate it after the first write?

Thanks!

Sub Append_Txt()

Dim oFS As FileSystemObject
Dim oFS1 As FileSystemObject
Dim oTS As TextStream
Dim oTS1 As TextStream
Dim vTemp

Dim Directory As String
'*************

Directory = "C:\New Folder\"
ChDir Directory

Set oFS = New FileSystemObject
Set oFS1 = New FileSystemObject

f = Dir(Directory, 7)

Do While f < ""

Set oTS = oFS.OpenTextFile(Directory & f, ForReading)
vTemp = oTS.ReadAll

Set oTS1 = oFS.OpenTextFile("C:\CombinedTemp.txt", ForAppending, True)
oTS1.Write (vTemp)
f = Dir
Loop
End Sub
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Appending a file

I don't see

oTS1.close
after the loop

And I don't see
oTS.close inside the loop--after you've done the .readall



Bythsx-Addagio wrote:

Hello,
I have the following code to combine the contents of all the text files in
one folder, into one single text file.

The problem is, I get an error 70 Permisson denied on the second time I try
to open the CombindedFile. I don't beleive the file is actually being
opened, but do I need to close it or deactivate it after the first write?

Thanks!

Sub Append_Txt()

Dim oFS As FileSystemObject
Dim oFS1 As FileSystemObject
Dim oTS As TextStream
Dim oTS1 As TextStream
Dim vTemp

Dim Directory As String
'*************

Directory = "C:\New Folder\"
ChDir Directory

Set oFS = New FileSystemObject
Set oFS1 = New FileSystemObject

f = Dir(Directory, 7)

Do While f < ""

Set oTS = oFS.OpenTextFile(Directory & f, ForReading)
vTemp = oTS.ReadAll

Set oTS1 = oFS.OpenTextFile("C:\CombinedTemp.txt", ForAppending, True)
oTS1.Write (vTemp)
f = Dir
Loop
End Sub


--

Dave Peterson
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27
Default Appending a file

Is this what you suggest? I still get the permission error when trying to
open the CombinedTemp.txt

Do While f < ""

Set oTS = oFS.OpenTextFile(Directory & f, ForReading)
vTemp = oTS.ReadAll
oTS.Close

Set oTS1 = oFS.OpenTextFile("C:\CombinedTemp.txt", ForAppending, True)
oTS1.Write (vTemp)


f = Dir
Loop
oTS1.Close





"Dave Peterson" wrote:

I don't see

oTS1.close
after the loop

And I don't see
oTS.close inside the loop--after you've done the .readall



Bythsx-Addagio wrote:

Hello,
I have the following code to combine the contents of all the text files in
one folder, into one single text file.

The problem is, I get an error 70 Permisson denied on the second time I try
to open the CombindedFile. I don't beleive the file is actually being
opened, but do I need to close it or deactivate it after the first write?

Thanks!

Sub Append_Txt()

Dim oFS As FileSystemObject
Dim oFS1 As FileSystemObject
Dim oTS As TextStream
Dim oTS1 As TextStream
Dim vTemp

Dim Directory As String
'*************

Directory = "C:\New Folder\"
ChDir Directory

Set oFS = New FileSystemObject
Set oFS1 = New FileSystemObject

f = Dir(Directory, 7)

Do While f < ""

Set oTS = oFS.OpenTextFile(Directory & f, ForReading)
vTemp = oTS.ReadAll

Set oTS1 = oFS.OpenTextFile("C:\CombinedTemp.txt", ForAppending, True)
oTS1.Write (vTemp)
f = Dir
Loop
End Sub


--

Dave Peterson

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Appending a file

I didn't notice that you were opening the output within the loop. Just opening
it once should be sufficient:

Option Explicit
Sub Append_Txt()

Dim oFS As FileSystemObject
Dim oTS As TextStream
Dim oTS1 As TextStream
Dim f As String
Dim vTemp As String
Dim myDirectory As String

myDirectory = "C:\my documents\excel\test\"
ChDrive myDirectory
ChDir myDirectory

Set oFS = New FileSystemObject

f = Dir(myDirectory, 7)

Set oTS1 = oFS.OpenTextFile("C:\CombinedTemp.txt", ForAppending, True)
Do While f < ""
Set oTS = oFS.OpenTextFile(myDirectory & f, ForReading)
vTemp = oTS.ReadAll
oTS.Close
oTS1.Write vTemp
f = Dir
Loop

oTS1.Close

End Sub

Ron de Bruin has alternate methods:
http://www.rondebruin.nl/csv.htm

Bythsx-Addagio wrote:

Is this what you suggest? I still get the permission error when trying to
open the CombinedTemp.txt

Do While f < ""

Set oTS = oFS.OpenTextFile(Directory & f, ForReading)
vTemp = oTS.ReadAll
oTS.Close

Set oTS1 = oFS.OpenTextFile("C:\CombinedTemp.txt", ForAppending, True)
oTS1.Write (vTemp)

f = Dir
Loop
oTS1.Close

"Dave Peterson" wrote:

I don't see

oTS1.close
after the loop

And I don't see
oTS.close inside the loop--after you've done the .readall



Bythsx-Addagio wrote:

Hello,
I have the following code to combine the contents of all the text files in
one folder, into one single text file.

The problem is, I get an error 70 Permisson denied on the second time I try
to open the CombindedFile. I don't beleive the file is actually being
opened, but do I need to close it or deactivate it after the first write?

Thanks!

Sub Append_Txt()

Dim oFS As FileSystemObject
Dim oFS1 As FileSystemObject
Dim oTS As TextStream
Dim oTS1 As TextStream
Dim vTemp

Dim Directory As String
'*************

Directory = "C:\New Folder\"
ChDir Directory

Set oFS = New FileSystemObject
Set oFS1 = New FileSystemObject

f = Dir(Directory, 7)

Do While f < ""

Set oTS = oFS.OpenTextFile(Directory & f, ForReading)
vTemp = oTS.ReadAll

Set oTS1 = oFS.OpenTextFile("C:\CombinedTemp.txt", ForAppending, True)
oTS1.Write (vTemp)
f = Dir
Loop
End Sub


--

Dave Peterson


--

Dave Peterson
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
appending one Excel file to another ? George Lewycky Excel Worksheet Functions 2 November 14th 08 07:37 PM
Appending to a file with run-time error '54' Bad file mode blisspikle Excel Programming 0 November 3rd 06 11:12 PM
appending to excel file [email protected] Excel Programming 0 May 29th 06 09:25 PM
Appending a second csv file Rick Excel Discussion (Misc queries) 3 March 9th 05 06:21 PM
Appending to File Progster Excel Programming 1 September 22nd 03 02:24 PM


All times are GMT +1. The time now is 09:05 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"