Link to home
Start Free TrialLog in
Avatar of mr_avril
mr_avril

asked on

How i'm gonna extract data from text logfile into VB?

Hi experts,

i have this kind of text logfile..

12/12/2003           09.01am            50mV
12/12/2002           09.02am            65mV
12/12/2003           09.03am            72mV

it have 3 columns (date, time , temp). Between date and time, time and temp.. they are seperated with one tab.

How i goona take data in each column at every line? meaning that, at first line, i want to take date, time and temp.
For date and time, i can take the whole string (12/12/2003, 09.01am) because it is not a value. For temp, i need the value to substitute into my formula in VB. so i need to seperate mV from the integer so that it would become a valid value. If that is difficult, how about i want to seperate this .. 50 mV <-- it has one white space between integer and unit. Can it be done?

Please help me..

thanks
ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of anv
anv

'if u can use commas to separate the columns the following code
'wud be easy to use..

Set rsTxt = New ADODB.Recordset
           
'fpath is the full path of the text file you want to open
'cn a connection object
            cn.Open "Provider=Microsoft.Jet" _
                    & ".OLEDB.4.0;Data Source=" & fpath _
                    & ";Extended Properties='text;HDR=NO;" _
                    & "FMT=Delimited'"

'fname is the Name of the text file to open

rsTxt.Open "select * from [" & fName & "#txt]", cn, adOpenStatic, _
                  adLockReadOnly, adCmdText
'now use the recordset to fetch data from the file
'as u use 4 DBMS..
Avatar of mr_avril

ASKER

sorry anv.. i'm very new to VB.. i don't understand what u means..
and idle_mind.. great coding.. but i cannot run it.. it give error..
What was the error?
Hi mr_avril,

This  code will read in file into a buffer and read one line at a time.Then it will oarse the line to find
your mV value.

Private Sub Parsing_to_New()
  Dim sRecord         As String
  Dim sParse          As String
  Dim sBuffer         As String
  Dim iReadfile       As Integer
  Dim iMyValue      As Integer
 
 
  'Open and read old file
  iReadfile = FreeFile
  Open "c:\YourFile.TXT" For Input As #iReadfile
  Do Until EOF(iReadfile)
     '******** read file into a buffer string ******************
     Select Case LOF(iReadfile) - Seek(iReadfile)
       Case 0
              If sParse = "" Then
                Exit Do
              End If
              If InStr(sParse, vbLf) = 0 Then
                Exit Do
              End If
      'This size is for a 1.44 Disc
      Case Is >= 142000
               sBuffer = Input(LOF(iReadfile) - Seek(iReadfile) + 1, iReadfile)
               sParse = sParse & sBuffer
      Case Is < 142000
              On Error Resume Next
              sBuffer = Input(LOF(iReadfile) - Seek(iReadfile) + 1, iReadfile)
              sParse = sParse & sBuffer
   End Select
    '*********** read string *******************************
   While InStr(sParse, vbLf)          '<----- Read one line at a time
       sRecord = Left(sParse, InStr(sParse, vbLf) - 1)  '<--- This will show you each line
       sParse = Mid(sParse, InStr(sParse, vbLf) + 1)    '<--- This will show you the whole file
       Debug.Print sRecord      '<---- Add to VB Watch to see how it works
       Debug.Print sParse      '<---- Add to VB Watch to see how it works
   If InStr(sRecord, "mV") Then
         iMyValue = Mid(sRecord, InStr(sRecord, "mV") - 2, 2)
   End If
   Print #iOutfile, sRecord
   Wend
Loop
     Close #iReadfile
  End Sub
I for got to remove line....
 
Print #iOutfile, sRecord

from the example code...
Please remove that line for the code to run without errors.