View Single Post
  #4   Report Post  
Posted to microsoft.public.excel.programming
Rich[_19_] Rich[_19_] is offline
external usenet poster
 
Posts: 7
Default Combine all text file in directory into one file.

Hi Tom,

I changed the code to what you have listed and I get a
Run-Time error '9': Subscript out of range. If I Debug it highlights:

Open "C:\ECPJM\" & SrcFiles(Counter) For Input As #2

Any ideas on this error? I greatly appreciate your help.

Thanks,

Rich


Sub a()
Dim SrcFiles() As String, CurrSrc As String
Dim DestFile As String, Counter As Integer
Dim TextLine As String, sName As String, i As Long
' SrcFiles = Array("c:\File1.txt", "c:\File2.txt")
ReDim SrcFiles(1 To 1000)
On Error Resume Next
Kill "C:\ECPJM\combined.txt"
On Error GoTo 0
sName = Dir("C:\ECPJM\" & "*.txt")
i = 0
Do While sName < ""
i = i + 1
SrcFiles(i) = sName
sName = Dir
Loop
ReDim SrcFiles(1 To i)
Open "c:\ECPJM\combined.txt" For Output As #1
For Counter = 0 To UBound(SrcFiles)
Open "C:\ECPJM\" & SrcFiles(Counter) For Input As #2
Do While Not EOF(2)
Line Input #2, TextLine
Print #1, TextLine
Loop
Close #2
Next
Close #1
End Sub




"Tom Ogilvy" wrote in message ...
Whoops, change

Open SrcFiles(Counter) For Input As #2

to

Open "C:\ECPJM\" & SrcFiles(Counter) For Input As #2

--
Regards,
Tom Ogilvy

Tom Ogilvy wrote in message
...
Sub a()
Dim SrcFiles() as String, CurrSrc As String
Dim DestFile As String, Counter As Integer
Dim TextLine As String, sName as String, i as Long
' SrcFiles = Array("c:\File1.txt", "c:\File2.txt")
Redim SrcFiles(1 to 1000)
On Error Resume Next
Kill "C:\ECPJM\combined.txt"
On Error goto 0
sName = Dir("C:\ECPJM\" & "*.txt")
i = 0
do while sName < ""
i = i + 1
SrcFiles(i) = sName
sName = Dir
Loop
Redim SrcFiles(1 to i)
Open "c:\ECPJM\combined.txt" For Output As #1
For Counter = 0 To UBound(SrcFiles)
Open SrcFiles(Counter) For Input As #2
Do While Not EOF(2)
Line Input #2, TextLine
Print #1, TextLine
Loop
Close #2
Next
Close #1
End Sub

--
Regards,
Tom Ogilvy

Rich wrote in message
om...
Hello,

I am trying to adapt the following code posted by Jim Rech.

Sub a()
Dim SrcFiles, CurrSrc As String
Dim DestFile As String, Counter As Integer
Dim TextLine As String
SrcFiles = Array("c:\File1.txt", "c:\File2.txt")
Open "c:\file3.txt" For Output As #1
For Counter = 0 To UBound(SrcFiles)
Open SrcFiles(Counter) For Input As #2
Do While Not EOF(2)
Line Input #2, TextLine
Print #1, TextLine
Loop
Close #2
Next
Close #1
End Sub

I need to do the following. I need to combine all the text files
in directory "C:\ECPJM" into one text file called "Combined.txt". Any
help would be greatly appreciated.

Thanks,

Rich