Alex Alex - 3 months ago 29
Vb.net Question

VB Read Line from File starting with "XY" and ignore the XY

Im currently working on a program which basicly writes a number of Files and their paths into a file & a new path to be used by another computer to put these files on its hard drive.
I don't know how to read the file properly by the second computer. (The "Receiving part" of the program)

The File which should be read looks like this:

OTHER STUFF
MORE STUFF
EVEN MORE STUFF
File1:~\\bar\foo.txt
Path1:~C:\foo
File2:~\\bar\foobar.txt
Path2:~C:\foobar
OTHER STUFF
MORE STUFF
EVEN MORE STUFF


How Can I read this file and only get the files and paths and exclude the "File" and "Path"?

I'm sorry if my english is not perfect, please ask if something it's not clear.
I hope you can help me.

Regards

This is what i got so far, but it will only display one character after the ~ (I use that as a seperator now)

Dim settings_file As String = "//test/test.txt"
Dim stream_reader As New StreamReader(settings_file)
Dim line As String
line = stream_reader.ReadLine()
Do While Not (line Is Nothing)
If line.StartsWith("File") Then
Dim parts As String() = line.Split("~")
For Each part In parts
MsgBox(part(1))
Next
End If
line = stream_reader.ReadLine()
Loop
stream_reader.Close()

Answer
Dim lines = File.ReadLines("test\test.txt")

Dim paths = From line In lines
            Where line Like "File#:~*" OrElse line Like "Path#:~*"
            Select Split(line, ":~", 2).Last

Dim result = paths.ToArray

or

Dim lines = File.ReadLines("test\test.txt")

Dim lookup = lines.ToLookup(Function(l) l.Substring(0, 4), Function(l) Split(l, ":~", 2).Last)

Dim files = lookup("File").ToArray
Dim paths = lookup("Path").ToArray
Comments