Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7
Default Combine all text file in directory into one file.

Harlan wrote in message ...
"Rich" wrote...

I changed it to the following and got it to work. Thanks for all the
help everyone.

Public Sub CombineTextFiles()
Dim SrcFiles() As String, CurrSrc As String
Dim DestFile As String, Counter As Integer
Dim TextLine As String, sName As String, i As Long
Dim SrcTag As String, DestDir As String

ReDim SrcFiles(1 To 1000)
DestFile = "C:\ECPJM\combined.txt"
DestDir = "C:\ECPJM\"

On Error Resume Next
Kill DestFile

On Error GoTo 0
SrcTag = "C:\ECPJM\*.txt"
sName = Dir(SrcTag)

i = 0
Do While sName < ""
i = i + 1
SrcFiles(i) = sName
sName = Dir
Loop
If i 0 Then
' Need preserve or you lose everything added to the array
ReDim Preserve SrcFiles(1 To i)
Else
MsgBox "There are no files that match " & SrcTag
Exit Sub
End If

On Error Resume Next
Open DestFile For Output As #1
If Err.Number < 0 Then
MsgBox "Destination file '" & DestFile & "' cannot be opened. Check"
to make sure file exists."

MsgBox "Destination file '" & DestFile & "' cannot be opened. Check to
make sure file exists."
Exit Sub
End If
For Counter = 1 To UBound(SrcFiles)
On Error Resume Next
Open DestDir & SrcFiles(Counter) For Input As #2
If Err.Number < 0 Then
MsgBox "File '" & DestDir & SrcFiles(Counter) & "' cannot be"
opened. Check to make sure file exists."

MsgBox "File '" & DestDir & SrcFiles(Counter) & "' cannot be opened.
Check to make sure file exists."
Exit Sub
End If
Do While Not EOF(2)
Line Input #2, TextLine
Print #1, TextLine
Loop
Close #2
Next
Close #1

MsgBox "Complete.", vbInformation
End Sub


I changed the code to the following, but am getting a run-time error
76 "Path not found" on the line:
Open "C:\ECPJM\" & SrcFiles(Counter) For Input As #2

However the path is spelled correctly and I even copied it from
Windows Explorer. Any ideas?


The problem may be interpretting 'path'. By 'path' do you mean just the
"C:\ECPJM\" piece, i.e., the directory path? If so, that's one problem. The term
'path' in this context means the file's full path name. Try adding a statement
just above this like

Debug.Print Counter, "C:\ECPJM\" & SrcFiles(Counter)

to see what the file's full pathname appears to be to Excel/VBA. I suspect the
segment

Do While sName < ""
i = i + 1
SrcFiles(i) = sName
sName = Dir
Loop
ReDim SrcFiles(1 To i)


is the source of the problem. Try changing the ReDim statement to

ReDim Preserve SrcFiles(1 To i)

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
file save error: the file or directory cannot be created dublg Excel Discussion (Misc queries) 1 September 25th 06 07:07 PM
Excel should let me sort the file directory when saving a file Beanee70 Excel Discussion (Misc queries) 0 March 14th 06 07:03 AM
How to get file name from long directory Lillian[_5_] Excel Programming 11 December 4th 03 06:49 PM
backing up file in directory [email protected] Excel Programming 6 November 9th 03 07:35 PM
get path - save new file - same sub-directory as existing file tegger Excel Programming 2 October 21st 03 10:45 AM


All times are GMT +1. The time now is 02:45 PM.

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

About Us

"It's about Microsoft Excel"