Link to home
Start Free TrialLog in
Avatar of Ken H.
Ken H.

asked on

Expression Help

If you follow my code below I have read all the lines in from the file already.

Now I need a way to collect just the preferred IP and hostnames from the data below. My thought was to check if the "PreferredFilerIPaddress" string exists in the file then grab the hostname(s) and ip(s) but I can't figure out how to do it.... i keep thinking loop but this is a single file so i don't think I'm on the right track.

[SYSTEM\CurrentControlSet\Services\SWSvc\Parameters\PreferredFilerIPAddresses]

FAS250-NI1                  10.10.1.201
FAS250-NI2                                           10.10.1.203
Dim SDDATALOG As String = txtPath.Text & "\SnapDriveInfo\SnapDrive_RegistryInfo.log"
Dim SnapDriveData As String() = IO.File.ReadAllLines(SDDATALOG)

Dim prefFlrIP = From p In SnapDriveData Where p.Contains(txtHostName.Text) Select p.Substring(0)

  Dim Row1 As DataRow
            Try
                Row1 = Table1.NewRow
                Row1.Item(PrefIP) = If(prefFlrIP.Count > 0, prefFlrIP.FirstOrDefault, "Not Set")
                Table1.Rows.Add(Row1)

            Catch ex As Exception

            End Try

Open in new window

Avatar of Ken H.
Ken H.

ASKER

Thinking about it more if I can figure out a way to just read the lines between these two into an array I think i can figure out the rest.

[SYSTEM\CurrentControlSet\Services\SWSvc\Parameters\PreferredFilerIPAddresses]

[SYSTEM\CurrentControlSet\Services\SWSvc\Parameters\Protocols]
Is these lines can appear multiple times in your file or only once ?
Avatar of Ken H.

ASKER

these lines appear only once in the file, and i'm only reading from this one file.
So I'd use to good old way.
Dim firstLine As String = "[SYSTEM\CurrentControlSet\Services\SWSvc\Parameters\PreferredFilerIPAddresses]"

Dim lastLine As String = "[SYSTEM\CurrentControlSet\Services\SWSvc\Parameters\Protocols]"


Dim SDDATALOG As String = txtPath.Text & "\SnapDriveInfo\SnapDrive_RegistryInfo.log"
Dim SnapDriveData As String = IO.File.ReadAllText()
Dim myData As String = ""

Dim firstLinePos As Integer = SnapDriveData.indexOf(firstLine)
If firstLinePos  <> -1 Then
  firstLinePos += firstLine.Length
  myData = SnapDriveData.Substring(firstLinePos, SnapDriveData.indexOf(lastLine ) - firstLinePos )
End If

Open in new window

Avatar of Ken H.

ASKER

This doesn't seem to be grabbing any data. I even tried just output to a text box and it's blank.

Ideally I want this data in the table listed in the code.


If Directory.Exists(txtPath.Text & "\SnapDriveInfo\") Then

            Dim SDVERLOG As String = txtPath.Text & "\SnapDriveInfo\SnapDriveVersionInfo.log"
            Dim SDDATALOG As String = txtPath.Text & "\SnapDriveInfo\SnapDrive_RegistryInfo.log"
            Dim firstLine As String = "[SYSTEM\CurrentControlSet\Services\SWSvc\Parameters\PreferredFilerIPAddresses]"
            Dim lastLine As String = "[SYSTEM\CurrentControlSet\Services\SWSvc\Parameters\Protocols]"

            Dim SnapDriveVersionInfo As String() = IO.File.ReadAllLines(SDVERLOG)
            Dim SnapDriveData As String() = IO.File.ReadAllLines(SDDATALOG)
            Dim SnapDriveData2 As String = IO.File.ReadAllText(SDDATALOG)
            Dim myData As String = ""

            Dim firstLinePos As Integer = SnapDriveData2.IndexOf(firstLine)
            If firstLinePos <> -1 Then
                firstLinePos += firstLine.Length
                myData = SnapDriveData2.Substring(firstLinePos, SnapDriveData2.IndexOf(lastLine) - firstLinePos)
            End If


            Dim sdVersion As String = SnapDriveVersionInfo(16).Substring(93)
            Dim sdService As String = SnapDriveData(20).Substring(26)
            'Dim sdService = From p In SnapDriveData Where p.Contains("ObjectName") Select p.Split("   ").Last

            Dim Row1 As DataRow
            Try
                Row1 = Table1.NewRow
                Row1.Item(Version) = sdVersion
                'Row1.Item(PrefIP) = If(myData.Count > 0, myData.FirstOrDefault, "Not Set")
                Row1.Item(PrefIP) = myData
                Row1.Item(ServiceAcct) = sdService
                Table1.Rows.Add(Row1)
                txtOut.Text = myData
            Catch ex As Exception

            End Try

        Else

        End If

Open in new window

Well.. Is the firstLine data really contained in the file ? Could you check if firstLine is found ? I mean to check the firstLinePos value. -1 meaning not found.
Avatar of Ken H.

ASKER

I have attached the log file I'm pulling the data from.

The lines look like this:

[SYSTEM\CurrentControlSet\Services\SWSvc\Parameters\PreferredFilerIPAddresses]

FAS250-NI1                  10.10.1.201
FAS250-NI2                  10.10.1.203

[SYSTEM\CurrentControlSet\Services\SWSvc\Parameters\Protocols]
SnapDrive-RegistryInfo.log
ASKER CERTIFIED SOLUTION
Avatar of djon2003
djon2003
Flag of Canada 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 Ken H.

ASKER

The encoding on the file isn't unicode. I don't know why but none of the files I have to parse are unicode which makes things more difficult. Maybe I should be using a streamreader instead?
Avatar of Ken H.

ASKER

This may be the action of last resort, i would have to open the file and save it to a temp directory in unicode format then open that file for reading.

I'm going to see if there is a way to do this with LINQ first.

Thank you for your effort in creating this solution for me!
If you know the encoding, the best would be to use it in the ReadAllX function you use.

Anyhow, to read it 100% correct, knowing the encoding is necessary.