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

LVL 1
newimagentAsked:
Who is Participating?
 
djon2003Connect With a Mentor Commented:
Ok, I made a small app which reads the file and extract the part you need.

It seems that the encoding of the file is not unicode because at first attempt the content of the file was << " >>. So I edited it in notepad and saved it as unicode and then it started to work !

Though the encoding problem may be due to other factors between me and you, ensuring that wouldn't be a bad idea.

If you can't change the encoding, use the good one by specifying it as the second param of a ReadAllX method.


In the app I kept the file in unicode in the bin directory. I had to remove all the binaries so compile it in debug mode (or move the file from the bin\debug).
WindowsApplication1.zip
0
 
newimagentAuthor Commented:
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]
0
 
djon2003Commented:
Is these lines can appear multiple times in your file or only once ?
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

 
newimagentAuthor Commented:
these lines appear only once in the file, and i'm only reading from this one file.
0
 
djon2003Commented:
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

0
 
newimagentAuthor Commented:
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

0
 
djon2003Commented:
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.
0
 
newimagentAuthor Commented:
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
0
 
newimagentAuthor Commented:
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?
0
 
newimagentAuthor Commented:
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!
0
 
djon2003Commented:
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.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.