-3

I need to go through a text file and check whether the start of each line begins with "Attribute". How should I do this in VB6?

CJ7
  • 22,579
  • 65
  • 193
  • 321
  • 2
    Again same as two of your [other](http://stackoverflow.com/questions/8206848/how-to-remove-all-code-from-multiple-vb6-frm-files-and-leave-form-design) [questions](http://stackoverflow.com/questions/8208131/pattern-match-processing-of-multiple-frm-files). If you want to expand on and clarify the question, expand on it, not start a full new question and answer thread. – Deanna Nov 21 '11 at 09:32

4 Answers4

2

Use a Regex. You will have to include the VBScript Regular Expressions library in your references.

Dim reg As new Scripting.Regex().
reg.Pattern = "^Attribute"
If reg.Match(line) Then
     ' Do Something
End If
Kris Erickson
  • 33,454
  • 26
  • 120
  • 175
1
Dim sInput As String, check as Boolean
check = true
Open "myfile" For INPUT As #txtFile
While Not EOF(txtFile)
   Input #txtFile, sInput
   If Not Mid(sInput,1,9) = "ATTRIBUTE" Then
       check = false
   End if
   sInput = ""
Wend
Close #txtFile

If check = true at the end, all lines start with "ATTRIBUTE", otherwise they do not.

Authman Apatira
  • 3,994
  • 1
  • 26
  • 33
1

You could try something like this (code not tested) -

Dim ParseDate, AllLinesStartWithAttribute, fso, fs
AllLinesStartWithAttribute = False
Set fso = CreateObject("Scripting.FileSystemObject")
Set fs = fso.OpenTextFile("c:\yourfile", 1, True)
Do Until fs.AtEndOfStream
    If Left(fs.ReadLine, 9) <> "Attribute" Then
       AllLinesStartWithAttribute = False
       Exit Do
    End If
Loop
fs.Close
Set fs = Nothing

Once the code is run if the AllLinesStartWithAttribute value is set to true then all lines in your file begin with 'Attribute'. Please note that this code is case sensitive.

ipr101
  • 24,096
  • 8
  • 59
  • 61
  • You can use 'Option Compare Text' at start of class/module to make it case insensitive. Then you can use operator Like "Attribute*" - makes code more readable. And you could terminate loop early when you discover that some line is invalid :) – Arvo Nov 21 '11 at 08:22
  • 2
    Posting code that will not compile when 'Option Explcit' is On is not recommended or good practise. – Matt Wilko Nov 21 '11 at 10:55
  • @Matt - Fair point, code has now been tidied add will compile with `Option Explicit` on. – ipr101 Nov 21 '11 at 11:19
0
Dim fso As New FileSystemObject
Dim ts As TextStream
Dim str As String

Set ts = fso.OpenTextFile(MyFile)
Do While Not ts.AtEndOfStream
    str = ts.ReadLine
    If InStr(str, "Attribute") = 1 Then
        ' do stuff
    End If
Loop
CJ7
  • 22,579
  • 65
  • 193
  • 321