extract specific text from a file using VB 2008

rs264

Newcomer
Joined
Oct 14, 2008
Messages
5
Hi, I'm a beginner to programming and i've looked all over the net for an answer and I can't find an answer to my problem. I found one that was close but coded in vb 6 and when I tried to upgrade the code with 2008 it wouldn't work and my debugging skills are...novice at best. Here's the text file I need to extract data from:
--------------------------------------------------
ftp> Connected to 10.100.50.25.

open 10.100.50.25
220 JD FTP Server Ready.
User (10.100.50.25:(none)):
331 Enter password.

230-Hewlett-Packard FTP Print Server Version 2.4.5

Directory: Description:
------------------------------------------------------
PORT1 Print to port 1 hp LaserJet 4250

To print a file, use the command: put <filename> [portx]
or 'cd' to a desired port and use: put <filename>.

Ready to print to PORT1

230 User logged in.
ftp> quit
221 Goodbye.

-------------------------------------------------------
This file is ftp data from an hp jetdirect printer and I've piped it to a text file
called log.txt. What I need to extract is just the name of the printer in question which here is listed on line 12 at character space 34 "hp laserjet 4250". All i need is to extract from line 12 starting at space 34 until the EOL and parse that string to another variable to use in a query of a printer database. The start of the string (hp) will always be on the same line and in the same place on the line it's just the rest of the string that varies depending on what jetdirect card I ftp to. I hope I've explained this clearly enough and that someone can either show me the code i need or point me in the right direction. Thanks in advance!:)
 
If the data is on the same line number, and in the same character position every time, this should get you started

Visual Basic:
Dim lines as String() = System.IO.File.ReadAllLines("log.txt")

For Each line as String in lines
    'Process line
    ' If line is 12, get printer name
    Console.WriteLine(line.Substring(34,lengthOfPrinterName))
Next
Alternatly, you could index directly to the 12th line, since lines is an array of type String.

Visual Basic:
lines(12).Substring(34,lengthOfPrinterName)
 
Thanks for the input, I tried the code you suggested (1st section) but I'm still not sure how to handle the "lengthofprintername" The actual length may not always be the same. The only constant in the printer name will be "hp" but the rest of the line may or may not be laserjet and the number will not always be the same length. When I tried your code and used the number 16 for the lengthofprintername (line.Substring(34,16) I received the following error
"A first chance exception of type 'System.ArgumentOutofRangeException' occured in mscorlib.dll. So I need to code for the length starting at "h" and just count to the end of line and then port that data to a text file.
Thanks in advance.
 
Substring with one argument

You can use Substring without specifying a length, in which case it will retrieve the substring up to the end of the original string:

Visual Basic:
Dim lines as String() = System.IO.File.ReadAllLines("log.txt")

'12th line is at index 11 - arrays are 0-based.
'34th character is at index 33 - strings are also 0-based.
Dim printerName As String = lines(11).Substring(33)

Good luck :cool:
 
A good point that a quick test of my code would have shown, my being to quick on the draw at the expense of accuracy ;)

Thats a known issue in Nate v1.0, I'm working on that bug fix all the time...
 
I tried your new code but I still receive the mscorlib.dll outofRangeException error. And can you also show me the code how to take the data extracted and parse it to a text file.
 
Can you post the code that is failing?

MrPauls code should work, if your "log.txt" file is not in the same directory as the binary, your array may be of length zero.

Try this

Visual Basic:
If System.IO.File.Exists("log.txt") Then
'... MrPaul's code
Else
    MessageBox.Show("File not found")
End If
 
Here's the code I have so far. Like I said from the beginning I'm just starting to learn to code vb. So what I did was to start a form and first use a button to get the ftp data and then use a second button with the code mr paul sent.
This is just preliminary to test the code until I workout the full program.

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Shell("cmd /c C:\windows\system32\ftp.exe -s:C:\1.txt > C:\log.ini", vbHide)
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim lines As String() = System.IO.File.ReadAllLines("c:\log.ini")
Dim printername As String = lines(11).Substring(33)
End Sub
End Class
 
I verified the file, but how do I parse the extracted data string to a text file or better yet to the clipboard.
 
Re: extract specific text from a file using a scripting language

So, if I understand correctly, you want to extract from file log.txt, line 12, from char 34 (including) to end of line. I am assuming that you count lines and chars from 1 (and not from 0 - a minor point).

THIS IS ELEMENTARY.

START_OF_CODE
var str file, line, extract
cat log.txt > $file # Get the file content into variable $file
lex "12" $file > $line # Get the 12th line into $line
chex "[34" $line > $extract # Get 34th char (included) onward into $extract
echo $extract > output.txt # Print the extracted portion from $extract to output file
END_OF_CODE

GIVE ME SOMETHING CHALLENGING !

I wrote the above in biterscripting (http://www.biterscripting.com for free download), but you can use any scripting language.

Randi
 
Back
Top