sir Yomet
help me sir i'm stuck with my project...my problem is how to open a multiple text file and load it to a single master file...i already have the code how to read multiple text file but the problem is when i write the text file it returns the file name that i open i.e. i open in "C:\textlog\" the file name is text1, text2, text3. it returns the file name with .txt at the end of the file, what i want to happen is to put the content of the all text files that i open in one master file....here's my sample code sir
Private Sub Form_Load()
Const START_PATH = "C:\Textlog\"
Dim sFileName As String
Dim sBuffer As String
Dim iFile As Integer
sFileName = Dir$(START_PATH & "*.txt")
Do While Len(sFileName) > 0
'Open/Read and Close
iFile = FreeFile
Open START_PATH & sFileName For Input As iFile
sBuffer = Input(LOF(iFile), iFile)
'Close iFile
'Replace all occurances
'Open/Write and close
iFile = FreeFile
Open "C:\output.txt" & sFileName For Output As iFile
Print #iFile, sBuffer
Close iFile
'Get the next file
sFileName = Dir$
Loop
End Sub
-----------------------------------------------------------
or do you have any idea how to insert a code to open a multiple text file
the code that you give to me sir, it's working nice, but again sir, it allows to open a 1 text file at a time...
my text files is located at "C:\textlog" please help with this sir, i don't have any idea to fix this sir..
Dim inFile As Integer
Dim InLine As String
Dim data() As String
Dim fields As Integer
Dim sFile As String
'sFile = txtFile.Text
sFile = Text1.Text
inFile = FreeFile
Open sFile For Input As #inFile
Set conn = New ADODB.Connection
conn.Open "DRIVER={MySQL ODBC 3.51 Driver};" & "SERVER=localhost;" & " DATABASE=time_and_attendance;" & "UID=root;PWD=admin; OPTION=3"
While Not EOF(inFile)
Line Input #inFile, InLine
If InStr(InLine, "Event:Access") > 0 Then
data() = Split(InLine)
fields = UBound(data())
Set rs = New ADODB.Recordset
squery = "": squery = "Select * from tblattendance where Date='" & data(0) & "' AND CardNumber='" & Card(data(5)) & "'"
rs.Open squery, conn
If rs.EOF Then
conn.Execute "INSERT INTO tblattendance (Date, CardNumber, TimeIn) " & _
"VALUES ('" & data(0) & "', " & _
"'" & Card(data(5)) & "', " & _
"'" & Format(Left(data(1), 8), "hh:mm:ss AM/PM") & "')"
Else
conn.Execute "UPDATE tblattendance set TimeOut = '" & Format(Left(data(1), 8), "hh:mm:ss AM/PM") & "' where Date='" & data(0) & "' AND CardNumber='" & Card(data(5)) & "'"
End If
rs.Close
Set rs = Nothing
End If
Wend
MsgBox "Record successfully saved ", vbInformation + vbOKOnly, "Record Saved"
Unload Me
Me.Show 1
Close #inFile
Exit Sub
Royaloba,
Like last time I will answer your question here in the public forum where everyone can use the information.
If I have understood you well you want to merge several text files into one. Is that correct?
If so your solution is actually quite simple, you simply
Dim oFile As Integer and use the FreeFile function to get a valid file number for it. Something like this:
Private Sub Form_Load()
Const START_PATH = "C:\Textlog\"
Dim sFileName As String
Dim sBuffer As String
Dim iFile As Integer
Dim oFile As Integer
oFile = FreeFile
Open "C:\Textlog\output.txt" For Output As oFile
sFileName = Dir$(START_PATH & "*.txt")
Do While Len(sFileName) > 0
'Open/Read and Close
iFile = FreeFile
Open START_PATH & sFileName For Input As iFile
sBuffer = Input(LOF(iFile), iFile)
Close iFile
'Replace all occurances
'Open/Write and close
' iFile = FreeFile
' Open "C:\output.txt" & sFileName For Output As iFile
' Print #iFile, sBuffer
' Close iFile
Print #oFile, sBuffer
'Get the next file
sFileName = Dir$
Loop
Close oFile
End Sub
What happens here is that you open the output file first and keep it open for the duration of the loop, therefore you can always write to it without overwriting what is already in it. The
Open <filename> For Output As <filenumber> actually overwrites a file if it exists already, you would that have to use the "For Append" in order to write without overwriting. However, if you keep the output file open all the time you don't have to worry about Output or Append.
In general you can open as many files as you want (almost) as long as you have a variable for each file you want to open, i.e. you
could have
Dim File1 As Integer
Dim File2 As Integer
Dim File3 As Integer
Dim File4 As Integer
Dim File5 As Integer
Dim File6 As Integer
...
...
...
Dim File100 As Integer
Dim File101 As Integer
Dim File102 As Integer
and then open each one of these files
at the same time for input, output, append or binary as you see fit. Now don't quote me on the exact number of files you can open at the same time, I don't know the exact number but the help file most probably does, otherwise someone in the forum does, I'm sure.
From what I can see the second code snippet you included was the solution to your first problem and is now working fine from what you said.
Hope this helps, if not just reply to this post and I will be notified.
Happy coding
Yomet