Grab a list of Text files and Append to a Master Text File. (1 Viewer)

Randomblink

The Irreverent Reverend
Local time
Today, 09:33
Joined
Jul 23, 2001
Messages
279
Ok, I am working with Travis in another area on this same problem, I thought I would drop it here since it is working it's way towards a VB situation anyway...

What I am trying to do is run code that will check a folder, grab all the text files in it, copy/append each text file into ONE mater file...

I have a form...
There are two fields...

field#1: [unb_folder_to_scan]
field#2: [unb_append_to]

Then I have a label captioned: Append Now
The label is named: btn_append_now

(I use labels for buttons since you can't tab to them...I learned that here folks...)

Ok...
On the On_Click event of my 'button' I have the following code that runs...


Private Sub btn_append_now_Click()
Dim strFileName As String
Dim aryFileList As Variant
' Loop through the Directory and Get a list of TXT files
MsgBox unb_folder_to_scan & " appended to " & unb_append_to, vbOKOnly

strFileName = Dir(unb_folder_to_scan & "*.txt")
ReDim aryFileList(0)
Do While strFileName <> ""
If aryFileList(UBound(aryFileList)) & "" <> "" Then
ReDim Preserve aryFileList(UBound(aryFileList) + 1)
End If
aryFileList(UBound(aryFileList)) = strFileName
strFileName = Dir
Loop

For intFileLoop = LBound(aryFileList) To UBound(aryFileList)
If procTheReadFile(aryFileList(intFileLoop), unb_append_to) = False Then
MsgBox "Error reading" & strFileName, vbOKOnly + vbInformation, Application.name
End If
Next intFileLoop
End Sub


The following is in a module, it is there to do what needs append the two files...


' This Function will grab the contents of text files and append it to the end of another one...
' call this function as so...
' procTheReadFile("Path & FileName of File to add","Path & FileName of Master")

Public Function procTheReadFile(ByVal strFile As String, ByVal strAppendToFile As String) As Boolean
On Error GoTo ErrorHandler
Dim intFileNum As Integer
Dim strText As String
Dim strOriginal As String
Dim strNew As String


'Loop through to get the text in the merge to file so that the strFile can be appended afterwords.
intFileNum = FreeFile
If Dir(strAppendToFile) <> "" Then
Open strAppendToFile For Input As intFileNum
'Loop until end of file.
Do While Not EOF(intFileNum)
Line Input #intFileNum, strText
MsgBox strOriginal & ": This is the text.", vbOKOnly
strOriginal = strOriginal & Chr(13) & Chr(10) & strText
Loop
Close intFileNum
Else
strOriginal = ""
End If

'Loop through to get the text in the strFile to file so that the strFile can be appended afterwords.
intFileNum = FreeFile
If Dir(strFile) <> "" Then
Open strFile For Input As intFileNum
'Loop until end of file.
Do While Not EOF(intFileNum)
Line Input #intFileNum, strText
strNew = strNew & Chr(13) & Chr(10) & strText
Loop
Close intFileNum
Else
strNew = ""
End If


Open strAppendToFile For Output As intFileNum
Print #intFileNum, strOriginal & (Chr(13) & Chr(10) + strNew)
Close #intFileNum

procTheReadFile = True 'Passed
MsgBox "procTheReadFile is True", vbOKOnly
Exit Function
ErrorHandler:
procTheReadFile = False 'Failed
MsgBox "procTheReadFile is False", vbOKOnly
Err.Clear

End Function


This is the function...

I put the message boxes in to follow the steps to find out where it is failing...

The message box that says [:This is the text] will grow larger and larger...(taller actually) but nothing but blank spaces is inside it...in other words it doesn't show any text...

So it looks like it isn't grabbing the text but instead is counting each line in that txt file...
I need it to go through each text file and copy/append them to my master file...

What I want to do for my test purposes is:

I enter "C:/My Documents" into the [unb_folder_to_scan] field...then...
I enter "C:/My Documents/test/Master.txt" into the [unb_append_to] field...then...
I press the 9btn_append_now] and it doesn't do what it is supposed to...

If someone can help it would be greatly appreciated...thank you thank you...
 

Travis

Registered User.
Local time
Today, 07:33
Joined
Dec 17, 1999
Messages
1,332
Change this code

strFileName = Dir(unb_folder_to_scan & "*.txt")
ReDim aryFileList(0)
Do While strFileName <> ""
If aryFileList(UBound(aryFileList)) & "" <> "" Then
ReDim Preserve aryFileList(UBound(aryFileList) + 1)
End If
aryFileList(UBound(aryFileList)) = strFileName
strFileName = Dir
Loop


To
If Right(unb_folder_to_scan,1) <>"\" then
unb_folder_to_scan = unb_folder_to_scan & "\"
End if
strFileName = Dir(unb_folder_to_scan & "*.txt")
ReDim aryFileList(0)
Do While strFileName <> ""
If aryFileList(UBound(aryFileList)) & "" <> "" Then
ReDim Preserve aryFileList(UBound(aryFileList) + 1)
End If
aryFileList(UBound(aryFileList)) = unb_folder_to_scan & strFileName
strFileName = Dir
Loop
 

Randomblink

The Irreverent Reverend
Local time
Today, 09:33
Joined
Jul 23, 2001
Messages
279
IT WORKS! IT WORKS! IT WORKS! IT WORKS! IT WORKS!IT WORKS! IT WORKS! IT WORKS! IT WORKS! IT WORKS!IT WORKS! IT WORKS! IT WORKS! IT WORKS! IT WORKS!IT WORKS! IT WORKS! IT WORKS! IT WORKS! IT WORKS!IT WORKS! IT WORKS! IT WORKS! IT WORKS! IT WORKS!IT WORKS! IT WORKS! IT WORKS! IT WORKS! IT WORKS!IT WORKS! IT WORKS! IT WORKS! IT WORKS! IT WORKS!IT WORKS! IT WORKS! IT WORKS! IT WORKS! IT WORKS!IT WORKS! IT WORKS! IT WORKS! IT WORKS! IT WORKS!IT WORKS! IT WORKS! IT WORKS! IT WORKS! IT WORKS!IT WORKS! IT WORKS! IT WORKS! IT WORKS! IT WORKS!IT WORKS! IT WORKS! IT WORKS! IT WORKS! IT WORKS!IT WORKS! IT WORKS! IT WORKS! IT WORKS! IT WORKS!

THANK YOU! THANK YOU! THANK YOU! THANK YOU! THANK YOU! THANK YOU! THANK YOU! THANK YOU! THANK YOU! THANK YOU! THANK YOU! THANK YOU! THANK YOU! THANK YOU! THANK YOU! THANK YOU! THANK YOU! THANK YOU! THANK YOU! THANK YOU! THANK YOU! THANK YOU! THANK YOU! THANK YOU! THANK YOU! THANK YOU!
 

Users who are viewing this thread

Top Bottom