2

If I have a text file that contains the below information, what is the best way to search for the version, for example on system1 only? (in vb6 code, can I use InStr?)

[system1]

version=xx
date=xx


[system2]

version=xx
date=xx
GSerg
  • 76,472
  • 17
  • 159
  • 346
user1179317
  • 2,693
  • 3
  • 34
  • 62

3 Answers3

1

The sample file looks like a standard INI file. You can read these using the GetPrivateProfileString() and related functions. You can also enumerate the sections and values using GetPrivateProfileSectionNames() and GetPrivateProfileSection().

Deanna
  • 23,876
  • 7
  • 71
  • 156
0

Supposing there is a space after the version and before date (str would be your text):

Dim version As String
version = Mid(str, InStr(str, "version") + 8)
version = Mid(version, 1, InStr(version, " "))

But there are a bunch of functions to parse .ini files out there.

Hanlet Escaño
  • 17,114
  • 8
  • 52
  • 75
0

I created a text file "C:\MYFILE.TXT" and this is the content of the file:

[system1]

version=aa
date=bb


[system2]

version=yy
date=zz

[system3]

date=4

[system4]

[system5]

As you can see, I made all possible situation, ie. a system with version info, a system with no version info, etc. Then I wrote this code:

Here is one way to look for a value under a categorized list:

Dim mySystem As String
Dim myVersion As String

mySystem = "[system2]"    'Replace this with the system you are looking for

Dim strTmp As String
Dim SystemFound As Boolean
Dim VersionFound As Boolean

Open "c:\myfilename.txt" For Input As #1
    Do While Not EOF(1)
        Line Input #1, strTmp
        If InStr(UCase(strTmp), UCase(mySystem)) > 0 Then
            SystemFound = True
            strTmp = ""
            Do While Not EOF(1) And InStr(strTmp, "[") = 0
                Line Input #1, strTmp
                If InStr(UCase(strTmp), "VERSION") > 0 Then
                    VersionFound = True
                    myVersion = Mid(strTmp, InStr(strTmp, "=") + 1, Len(strTmp))

                End If
            Loop
            Exit Do
        End If
    Loop
Close #1

If SystemFound And VersionFound Then
    MsgBox "The Version of " & mySystem & " is " & myVersion
ElseIf SystemFound And Not VersionFound Then
    MsgBox "The system " & mySystem & " has no version definition"
ElseIf SystemFound = False Then
    MsgBox "The system " & mySystem & " is not found in file"
End If
Ahmad
  • 12,336
  • 6
  • 48
  • 88