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
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
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()
.
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.
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