0
votes

Macro pulls incorrect data from txt files. I have a code that loops through several hundred files pulling timestamps for start time and Diagnose time and pastes them into column A and B. The time stamp for start time pulls correctly but the time stamp for Diagnose does not. Instead the first line of text in the txt file is pulled and pasted into column B. A sample of the input txt Log file looks like this, there are hundreds of other time stamps in the txt log file but the two time stamps I care about are start and [irp] Diagnose

      +version=LogbookPlus 1.7.23
      +site=
      +lastedit=2019-08-31 17:19:31.289
      +description=SRC - LSA-0251 error
      +number=1282
      +so=51657136
      +toolowner=
      +init=2019-08-30 08:40:38.360
      +start=2019-08-30 08:25
      +end=2019-08-30 09:45
      +down=Unscheduled
      +account=Source
      +rooterror=LSA-0251
      +subsystem=ILP-DC-PQ
      +assy=Dose & power performance
      +work=2019-08-30|08:39| [IRP] Diagnose
      +work=2019-08-30|08:41| Start streaming
      +work=2019-08-30|09:03| Conditioning
      +work=2019-08-30|09:04| Standby

Here is my code

Sub FindTimeStamps()

Dim TextFile As Integer
Dim FilePath As String
Dim FileContent As String
Dim MyFolder As String, MyFile As String

'Open Diaglouge box prompting user to choose folder path

With Application.FileDialog(msoFileDialogFolderPicker)
    .AllowMultiSelect = False
    .Show
    MyFolder = .SelectedItems(1)
    Err.Clear
End With

'Create a new object for files in that folder and apply for/loop

Dim objFSO As Object
Dim objFolder As Object
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.getFolder(MyFolder)
Dim fls As Object
Dim i As Integer
i = 1
For Each fls In objFolder.Files
'File Path of Text File

MyFile = MyFolder & "\" & fls.Name

'Determine the next file number available for use by the Fileopen function

TextFile = FreeFile
'open the text file

Debug.Print CurDir
Open MyFile For Input As #1

'Store file content inside a variable

 Do Until EOF(1)
    Line Input #1, textline
    Text = Text & textline
Loop

Close #1
 'Find Time Stamp Data from txt file
  Dtime = Diagnose
  Diagnose = InStr(1, Text, Dtime)
  dt = Mid(Text, Diagnose + 1, 17)
  Sttime = InStr(Text, "+start=")

'Paste obtained Time Stamp into excel Cells

Range("A" & i + 1).Value = Mid(Text, Sttime + 7, 16)
Range("B" & i + 1).Value = dt
i = i + 1

Text = ""
Next
End Sub

if I do not hard code the Diagnose variable and instead do a user input such as

Find = InputBox("which word")
Open Text For Input As #1
Do While Not EOF(1)
   Input #1, Text
   If InStr(1, Text, Find) > 0 Then
      idx = InStr(1, Text, "=")
      dt = Mid(Text, idx + 1, 17)
   Exit Do
End If
Loop

This was suggested by another user and the code works and pulls the correct time stamp. The Drawback to this is that I have to keep typing in Diagnose for every single file in the folder which is not ideal. Im still learning VBA so I'm not just looking for a solution but also a reason why the hard coded variable does not pull data correctly. Thanks any help is much appreciated

Here is the output of the macro I get in excel when I use the Hard coded variable for Diagnose, there are no time stamps in column b for diagnose it just pulls the first line of the input txt file for reason I do not understand

          Column A          Column B
          8/28/2019 14:29   version=LogbookPl
          8/29/2019 5:38    version=LogbookPl
          8/30/2019 8:25    version=LogbookPl
2
It is not very clear as you are not exposing which one is the input data and which one is your expected output. Anyhow, if you need to work always with "Diagnose", instead to place a InputBox, just set Find="Diagnose". - David García Bodego
Sorry I just posted the output I keep getting the first column is the time stamp of the start time and the second Column is supposed to be the time stamp of diagnose but instead is retrieves version=logbook. Also I’ve already tried setting a hard variable such as find=“diagnose” but it doesn’t find the word diagnose in the text file - user11536789
That´s why, could you edit and place a small example of your input data? - David García Bodego
I edited the question to point out the input data and the output that I keep getting with my current code - user11536789
ahhh that makes sense. So i need to somehow specify i want to find Diagnose within the [IRP] Diagnose string in the txt file input and pull out the time stamp associated with it - user11536789

2 Answers

2
votes

Try this one.

I check that one in a couple of files and it works.

Sub FindTimeStamps()

Dim TextFile As Integer
Dim FilePath As String
Dim FileContent As String
Dim MyFolder As String, MyFile As String

'Open Diaglouge box prompting user to choose folder path

With Application.FileDialog(msoFileDialogFolderPicker)
    .AllowMultiSelect = False
    .Show
    MyFolder = .SelectedItems(1)
    Err.Clear
End With

'Create a new object for files in that folder and apply for/loop

Dim objFSO As Object
Dim objFolder As Object
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.getFolder(MyFolder)
Dim fls As Object
Dim i As Integer
i = 1
For Each fls In objFolder.Files
'File Path of Text File

MyFile = MyFolder & "\" & fls.Name

'Determine the next file number available for use by the Fileopen function

TextFile = FreeFile
'open the text file

Debug.Print CurDir
Open MyFile For Input As #1

'Store file content inside a variable

Do Until EOF(1)
    Input #1, textline
    Text = textline
    If (InStr(Text, "+start=") <> 0) Then
        Sttime = InStr(Text, "+start=")
        Range("A" & i + 1).Value = Mid(Text, Sttime + 7, 16)
    End If
    If (InStr(Text, "Diagnose") <> 0) Then
        dt = InStr(Text, "Diagnose")
        Range("B" & i + 1).Value = Mid(Text, dt - 24, 10) & " " & Mid(Text, dt - 13, 5)
        i = i + 1
    End If
Loop

Close #1

Next
End Sub

Hope it helps

With the Input you gave: (I just repeat it)

+version=LogbookPlus 1.7.23
 +start=2019-08-30 08:25
 +work=2019-08-30|08:41| [IRP] Diagnose
 +work=2019-09-08|14:32| DAS power on
 +work=2019-09-08|14:33| linux boot
 +version=LogbookPlus 1.7.23
 +start=2019-08-30 08:25
 +work=2019-08-30|08:41| [IRP] Diagnose
 +work=2019-09-08|14:32| DAS power on
 +work=2019-09-08|14:33| linux boot
 +version=LogbookPlus 1.7.23
 +start=2019-08-30 08:25
 +work=2019-08-30|08:41| [IRP] Diagnose
 +work=2019-09-08|14:32| DAS power on
 +work=2019-09-08|14:33| linux boot
 +version=LogbookPlus 1.7.23
 +start=2019-08-30 08:25
 +work=2019-08-30|08:41| [IRP] Diagnose
 +work=2019-09-08|14:32| DAS power on
 +work=2019-09-08|14:33| linux boot

I am gettting:

This log

Is it this what you are looking for?

1
votes

Try this one:

Sub FindTimeStamps()

Dim TextFile As Integer
Dim FilePath As String
Dim FileContent As String
Dim MyFolder As String, MyFile As String

'Open Diaglouge box prompting user to choose folder path

With Application.FileDialog(msoFileDialogFolderPicker)
    .AllowMultiSelect = False
    .Show
    MyFolder = .SelectedItems(1)
    Err.Clear
End With

'Create a new object for files in that folder and apply for/loop

Dim objFSO As Object
Dim objFolder As Object
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.getFolder(MyFolder)
Dim fls As Object
Dim i As Integer
i = 1
For Each fls In objFolder.Files
'File Path of Text File

MyFile = MyFolder & "\" & fls.Name

'Determine the next file number available for use by the Fileopen function

TextFile = FreeFile
'open the text file

Debug.Print CurDir
Open MyFile For Input As #1

'Store file content inside a variable

Do Until EOF(1)
    Input #1, textline
    Text = textline
    If (InStr(Text, "+start=") <> 0) Then
        Sttime = InStr(Text, "+start=")
        Range("A" & i + 1).Value2 = Mid(Text, Sttime + 7, 16)
        i = i + 1
    End If
    If (InStr(Text, "| [IRP] Diagnose") <> 0) Then
        dt = InStr(Text, "| [IRP] Diagnose")
        Range("B" & i).Value2 = Mid(Text, dt - 16, 10) & " " & Mid(Text, dt - 5, 5)
    End If
    If (Range("B" & i).Value2 = "") And (Range("A" & i).Value2 <> "") Then _
                                        Range("B" & i).Value2 = "No Diagnose"
Loop
Close #1

Next
End Sub

With the files you sent me it is working. Please check