Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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!:)

Posted

If the data is on the same line number, and in the same character position every time, this should get you started

 

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.

 

lines(12).Substring(34,lengthOfPrinterName)

~Nate�

___________________________________________

Please use the [vb]/[cs] tags on posted code.

Please post solutions you find somewhere else.

Follow me on Twitter here.

Posted

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.

Posted

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:

 

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:

Never trouble another for what you can do for yourself.
Posted

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...

~Nate�

___________________________________________

Please use the [vb]/[cs] tags on posted code.

Please post solutions you find somewhere else.

Follow me on Twitter here.

Posted
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.
Posted

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

 

If System.IO.File.Exists("log.txt") Then
'... MrPaul's code
Else
   MessageBox.Show("File not found")
End If

~Nate�

___________________________________________

Please use the [vb]/[cs] tags on posted code.

Please post solutions you find somewhere else.

Follow me on Twitter here.

Posted

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

Posted

Have you verified the output of the .ini file?

 

By all accounts that looks like it should work.

~Nate�

___________________________________________

Please use the [vb]/[cs] tags on posted code.

Please post solutions you find somewhere else.

Follow me on Twitter here.

  • 4 months later...
Posted

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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...