User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Visual Basic 4 / 5 / 6 section within the Software Development category of DaniWeb, a massive community of 401,948 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,366 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Views: 5825 | Replies: 26
Reply
Join Date: Sep 2006
Posts: 50
Reputation: royaloba is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 1
royaloba royaloba is offline Offline
Junior Poster in Training

Help How to open multiple textfile

  #1  
Oct 11th, 2006
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, 22 views)
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Nov 2005
Location: Montreal, QC (Almost)
Posts: 130
Reputation: Yomet is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 9
Yomet Yomet is offline Offline
Junior Poster

Re: How to open multiple textfile

  #2  
Nov 10th, 2006
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

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
Reply With Quote  
Join Date: Nov 2006
Posts: 711
Reputation: QVeen72 is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 97
QVeen72's Avatar
QVeen72 QVeen72 is offline Offline
Master Poster

Re: How to open multiple textfile

  #3  
Nov 11th, 2006
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
Reply With Quote  
Join Date: Sep 2006
Posts: 50
Reputation: royaloba is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 1
royaloba royaloba is offline Offline
Junior Poster in Training

Re: How to open multiple textfile

  #4  
Nov 11th, 2006
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, 11 views)
Reply With Quote  
Join Date: Nov 2006
Posts: 711
Reputation: QVeen72 is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 97
QVeen72's Avatar
QVeen72 QVeen72 is offline Offline
Master Poster

Re: How to open multiple textfile

  #5  
Nov 11th, 2006
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
Reply With Quote  
Join Date: Nov 2006
Posts: 711
Reputation: QVeen72 is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 97
QVeen72's Avatar
QVeen72 QVeen72 is offline Offline
Master Poster

Re: How to open multiple textfile

  #6  
Nov 11th, 2006
Hi,

Give

Loop Until MyDate > ToDate

Remove Equal Sign,

Regards
Veena
Reply With Quote  
Join Date: Sep 2006
Posts: 50
Reputation: royaloba is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 1
royaloba royaloba is offline Offline
Junior Poster in Training

Re: How to open multiple textfile

  #7  
Nov 12th, 2006
Veena,
it's working very well, thanks to your help!!!
Reply With Quote  
Join Date: Sep 2006
Posts: 50
Reputation: royaloba is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 1
royaloba royaloba is offline Offline
Junior Poster in Training

Help Re: How to open multiple textfile

  #8  
Nov 27th, 2006
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, 7 views)
Reply With Quote  
Join Date: Nov 2006
Posts: 711
Reputation: QVeen72 is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 97
QVeen72's Avatar
QVeen72 QVeen72 is offline Offline
Master Poster

Re: How to open multiple textfile

  #9  
Nov 27th, 2006
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
Reply With Quote  
Join Date: Nov 2006
Posts: 711
Reputation: QVeen72 is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 97
QVeen72's Avatar
QVeen72 QVeen72 is offline Offline
Master Poster

Re: How to open multiple textfile

  #10  
Nov 27th, 2006
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
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Visual Basic 4 / 5 / 6 Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Other Threads in the Visual Basic 4 / 5 / 6 Forum

All times are GMT -4. The time now is 5:22 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC