I’m wondering if you put in the question-marks (re.pattern), which I don’t really understand or if this forum changed some characters to it? I’ve had many characters changed in the VBA section here.
#Vb net 2010 read text file line by line code#
I’ll take a look at the code you mentioned. I see what you mean about the Line Input. means match a periodįrom: MrkFrrl via vb-dotnet-l Sent: Wednesday, J10:49 AM To: Tonko Subject: RE: How do you read parts of text file line with VB? i used that emulation more than years ago, to which i sent remote scripts from an alpha server with vms.ĭim re As Object Dim ipAddresses() as String Dim ipMatch as String Set re = CreateObject(“VBScript.RegExp”) Sub ImportLineVariables() Dim Raw1$, Raw2$ Open “ServerNamePathtest.txt” For Input As #1 Line Input #1, Raw1 ‘ “IP: ” = 4 characters If len(Raw1) > 4 then Raw1 = Right(Raw1, Len(Raw1) – 4) MsgBox Raw1 Line Input #1, Raw2 ‘ “Machine: ” = 9 characters If Len(Raw2) > 9 Then Raw2 = Right(Raw2, Len(Raw2) – 9) MsgBox Raw2 Close #1 End Sub I see what I was doing wrong: While I thought that I’d use sequential numbers for Line Input, each successive line maintains the same Line Input #1. Posted by MrkFrrl on Jul 12 at 2:59 PM Mark as helpful If you look at the code, it uses #1 as a reference to the file when you open, read, and close the file.Īs you have seen, Line Input reads the next line in the file each time you call it.įrom: MrkFrrl via vb-dotnet-l Sent: Monday, J3:00 PM To: hdsrob Subject: RE: How do you read parts of text file line with VB? #1 is the file handle that is being used to gain access to the file, not the line number. I know you solved the issue, just thought you might want an explanation.
Sub ImportLineVariables() Dim Raw1$, Raw2$, Raw3$ Open “ServerNamePathtest.txt” For Input As #1 Line Input #1, Raw1 ‘ “IP: ” = 4 characters Raw1 = Right(Raw1, Len(Raw1) – 4) MsgBox Raw1 Line Input #2, Raw2 ‘ “Machine: ” = 9 characters Raw2 = Right(Raw2, Len(Raw2) – 9) MsgBox Raw2 End Sub I can get it to read my first line fine using Line Input, but the sequential lines produce errors. So, anything after the colon-space (“: “) but before the carriage return – char(13) & char(10) – for each line would be my variable (Raw1 = first partial line variable, Raw2 = second partial line variable, etc.). I want to import the “x”s (and periods between them) as separate variables. For example, the text file’s first line might be this: “IP: xx.xxx.xx.xxx,” and the next line might be, “Machine: Xxxxx,” followed by, “Alt IP: xx.xx.xxx.xxx”. While I can read the whole text file, I want to later just be able to read parts of the line as distinct variables.
I have an application that creates a text file, which is then saved. if you read line by line and store these lines in a string looking for ips, i recommend you use regular expressions to find these ip addresses.įrom: MrkFrrl via vb-dotnet-l Sent: Monday, J12:33 PM To: Tonci Korsano Subject: How do you read parts of text file line with VB? I would say streamreader is more suitable for small text files, like 64kb or less. I would recommend using textreader or streamreader classes to read these text files. Open “”ServerNamePathtest.txt”” For Input As #1Īlso, how does the Print command relate to this? I’ve seen it used by someone else but don’t fully understand its function. So, anything after the colon-space (“”: “”) but before the carriage return – char(13) & char(10) – for each line would be my variable (Raw1 = first partial line variable, Raw2 = second partial line variable, etc.).
I want to import the “”x””s (and periods between them) as separate variables. For example, the text file’s first line might be this: “”IP: xx.xxx.xx.xxx,”” and the next line might be, “”Machine: Xxxxx,”” followed by, “”Alt IP: xx.xx.xxx.xxx””.