944,144 Members | Top Members by Rank

Ad:
You are currently viewing page 1 of this multi-page discussion thread
Oct 11th, 2006
0

How to open multiple textfile

Expand Post »
hello every one..
good day...
first i was able find the solution in this site on how to open the textfile and save it to the
database, i'm using mysql as my database...thanks to sir "jomet" . now what i want to do is, i want to
open a muntiple text file and save it to database...my project is to create a time and attendance
system base on the textlog creates by biometic time and attendance device...this device creates a
daily textlog which the file name base on date i.e.10102006, 10112006, 10122006... (that is the exact
format of the filename) as i mention i was able to open a textfile but, one textfile only..what i want
to do is, i will create a datepicker then, when i select a date i.e october 10 to 12.. all the
textfile that is in the range of the selected date, and it will save to the database...
the textlog is located at C:\textlog\... please give me a sample code, as my guide..i need help guru..

try to see the sample textlog bellow
Attached Files
File Type: zip TextLog.zip (3.3 KB, 144 views)
Reputation Points: 13
Solved Threads: 1
Junior Poster in Training
royaloba is offline Offline
54 posts
since Sep 2006
Nov 11th, 2006
0

Re: How to open multiple textfile

Quote originally posted by royaloba in PM ...
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

Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. Private Sub Form_Load()
  2. Const START_PATH = "C:\Textlog\"
  3. Dim sFileName As String
  4. Dim sBuffer As String
  5. Dim iFile As Integer
  6.  
  7. sFileName = Dir$(START_PATH & "*.txt")
  8. Do While Len(sFileName) > 0
  9. 'Open/Read and Close
  10. iFile = FreeFile
  11. Open START_PATH & sFileName For Input As iFile
  12. sBuffer = Input(LOF(iFile), iFile)
  13. 'Close iFile
  14. 'Replace all occurances
  15. 'Open/Write and close
  16. iFile = FreeFile
  17. Open "C:\output.txt" & sFileName For Output As iFile
  18. Print #iFile, sBuffer
  19. Close iFile
  20. 'Get the next file
  21. sFileName = Dir$
  22. Loop
  23. 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..

Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. Dim inFile As Integer
  2. Dim InLine As String
  3. Dim data() As String
  4. Dim fields As Integer
  5. Dim sFile As String
  6. 'sFile = txtFile.Text
  7. sFile = Text1.Text
  8. inFile = FreeFile
  9.  
  10. Open sFile For Input As #inFile
  11. Set conn = New ADODB.Connection
  12. conn.Open "DRIVER={MySQL ODBC 3.51 Driver};" & "SERVER=localhost;" & " DATABASE=time_and_attendance;" & "UID=root;PWD=admin; OPTION=3"
  13. While Not EOF(inFile)
  14. Line Input #inFile, InLine
  15. If InStr(InLine, "Event:Access") > 0 Then
  16. data() = Split(InLine)
  17. fields = UBound(data())
  18.  
  19. Set rs = New ADODB.Recordset
  20. squery = "": squery = "Select * from tblattendance where Date='" & data(0) & "' AND CardNumber='" & Card(data(5)) & "'"
  21. rs.Open squery, conn
  22. If rs.EOF Then
  23. conn.Execute "INSERT INTO tblattendance (Date, CardNumber, TimeIn) " & _
  24. "VALUES ('" & data(0) & "', " & _
  25. "'" & Card(data(5)) & "', " & _
  26. "'" & Format(Left(data(1), 8), "hh:mm:ss AM/PM") & "')"
  27.  
  28.  
  29. Else
  30. 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)) & "'"
  31. End If
  32. rs.Close
  33. Set rs = Nothing
  34. End If
  35. Wend
  36.  
  37. MsgBox "Record successfully saved ", vbInformation + vbOKOnly, "Record Saved"
  38. Unload Me
  39. Me.Show 1
  40. Close #inFile
  41. 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:
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. Private Sub Form_Load()
  2. Const START_PATH = "C:\Textlog\"
  3. Dim sFileName As String
  4. Dim sBuffer As String
  5. Dim iFile As Integer
  6. Dim oFile As Integer
  7.  
  8. oFile = FreeFile
  9. Open "C:\Textlog\output.txt" For Output As oFile
  10. sFileName = Dir$(START_PATH & "*.txt")
  11. Do While Len(sFileName) > 0
  12. 'Open/Read and Close
  13. iFile = FreeFile
  14. Open START_PATH & sFileName For Input As iFile
  15. sBuffer = Input(LOF(iFile), iFile)
  16. Close iFile
  17. 'Replace all occurances
  18. 'Open/Write and close
  19. ' iFile = FreeFile
  20. ' Open "C:\output.txt" & sFileName For Output As iFile
  21. ' Print #iFile, sBuffer
  22. ' Close iFile
  23. Print #oFile, sBuffer
  24. 'Get the next file
  25. sFileName = Dir$
  26. Loop
  27. Close oFile
  28. 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
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. Dim File1 As Integer
  2. Dim File2 As Integer
  3. Dim File3 As Integer
  4. Dim File4 As Integer
  5. Dim File5 As Integer
  6. Dim File6 As Integer
  7. ...
  8. ...
  9. ...
  10. Dim File100 As Integer
  11. Dim File101 As Integer
  12. 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
Reputation Points: 16
Solved Threads: 10
Junior Poster
Yomet is offline Offline
134 posts
since Nov 2005
Nov 11th, 2006
0

Re: How to open multiple textfile

Hi,

Say ur datepicker is dtpk_F, dtpk_T
code u need to write is,

Dim FromDate As Date
Dim ToDate As Date
Dim MyDate As Date
Dim MyFile As String
Dim FNo As Long

FromDate = Format(dtpk_F.Value,"dd-mm-yyyy")
ToDate = Format(dtpk_T.Value,"dd-mm-yyyy")
MyDate = FromDate
Do
MyFile = "C:\textlog\" & Format(MyDate,"ddmmyyyy") & ".txt"
FNo = FreeFile
Open MyFile For Input As FNo
' Do ur processing here
Close FNo
'
MyDate = MyDate + 1
'
Loop Until MyDate >= ToDate

If u have doubts, u can get back to me.

Regards
Veena
Reputation Points: 84
Solved Threads: 140
Posting Shark
QVeen72 is offline Offline
923 posts
since Nov 2006
Nov 11th, 2006
0

Re: How to open multiple textfile

veena,
thanks for you quick response.... i got the rigth output that i want.. but there is only few bugs...
first when i select a date i.e. 11012006 to 11302006 there is a error, when the textfile is not present in the textlog directory i.e. 11052006, 11082006... and if i select a date 11012006 to 11302006 the return value is up to 11292006 only, i will attach the sample
thanks again
Attached Files
File Type: zip Sample.zip (43.2 KB, 82 views)
Reputation Points: 13
Solved Threads: 1
Junior Poster in Training
royaloba is offline Offline
54 posts
since Sep 2006
Nov 11th, 2006
0

Re: How to open multiple textfile

Hi,

To check if file is present or not, u can use this code,

If Dir(MyFile, vbDirectory) <> "" Then
' Open File Here
' Do Text Processing Here
' Close File Here
End If

Regards
Veena
Reputation Points: 84
Solved Threads: 140
Posting Shark
QVeen72 is offline Offline
923 posts
since Nov 2006
Nov 11th, 2006
0

Re: How to open multiple textfile

Hi,

Give

Loop Until MyDate > ToDate

Remove Equal Sign,

Regards
Veena
Reputation Points: 84
Solved Threads: 140
Posting Shark
QVeen72 is offline Offline
923 posts
since Nov 2006
Nov 12th, 2006
0

Re: How to open multiple textfile

Veena,
it's working very well, thanks to your help!!!
Reputation Points: 13
Solved Threads: 1
Junior Poster in Training
royaloba is offline Offline
54 posts
since Sep 2006
Nov 27th, 2006
0

Re: How to open multiple textfile

Veena,
hello, i add a field in the database named total TotalHours, i add this to compute the difference time of TimeIn and TimeOut, but i have a hardtime doing it, i didn't know where is my error try to see the sample. hope you can help me again..thanks
Attached Files
File Type: zip Sampledb.zip (139.2 KB, 62 views)
Reputation Points: 13
Solved Threads: 1
Junior Poster in Training
royaloba is offline Offline
54 posts
since Sep 2006
Nov 27th, 2006
0

Re: How to open multiple textfile

Hi,

U have To create TotalHours As a Date/Time Field Only.

I have Access 97 here , so was not able to open ur Database.

Any way try this SQL Statement to calculate Total Hours:

Update tblAttandence Set TotalHours = CDate(TimeOut) - CDate(TimeIN) Where TimeOut IS Not Null And Date=#01-01-2005' And CardNo='5'

If u still have problems, feel free to contact me.

Regards
Veena
Reputation Points: 84
Solved Threads: 140
Posting Shark
QVeen72 is offline Offline
923 posts
since Nov 2006
Nov 27th, 2006
0

Re: How to open multiple textfile

Hi,
Sorry that was

Update tblAttandence Set TotalHours = CDate(TimeOut) - CDate(TimeIN) Where TimeOut IS Not Null And Date=#01-01-2005# And CardNo='5'

Regards
Veena
Reputation Points: 84
Solved Threads: 140
Posting Shark
QVeen72 is offline Offline
923 posts
since Nov 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Visual Basic 4 / 5 / 6 Forum Timeline: i need help
Next Thread in Visual Basic 4 / 5 / 6 Forum Timeline: Editing Msgbox in visualbasic 6





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC