PDA

View Full Version : Extracting text from Textstream based on criteria


Brad
12-13-2002, 11:19 AM
Good morning,

I'm trying to extract certain lines from a textstream, but can't work out how to do it. Below is a paragraph from the file i'm using:

Firstname_Lastname Password = "testpassword"
Service-Type=Framed,
Framed-protocol=MPP,
Ascend-Route-IP=Route-IP-Yes,
Ascend-Data-Svc = Switched-128K,
Ascend-Metric=1,
Framed-Routing=None,
Ascend-Idle-Limit = 300,
Ascend-Assign-IP-Pool=1

The file is basically a long list of such paragraphs.

I'd like to extract and display only those lines which contain the Password section (ie the top line of each paragraph. Is there a way to do this ? I started as below, but it's not working...


Set fs=Server.CreateObject("Scripting.FileSystemObject")
Set f=fs.OpenTextFile(Server.MapPath("users.txt"), 1)

do while f.AtEndOfStream = false
if f.Readline = "'%" & "password" & "'" Then
Response.Write(f.ReadLine)
Response.Write("<br>")
end if
loop

f.Close
Set f=Nothing
Set fs=Nothing

Alternatively, I've linked the text file into an MSAccess database, and would like to extract the lines which = the same...again, i've started, but it ain't working...

set objrs = server.createobject("ADODB.recordset")

password = " password"
SQLStr = "SELECT Users.Field1 FROM Users WHERE (((Users.Field1) Like '%" & password & "'));"
objrs.open SQLStr, objconn
response.write SQLStr

Thanks a lot in advance,
Brad.

whammy
12-13-2002, 09:40 PM
Try using the InStr() function to see if password is in the line.

oracleguy
12-13-2002, 09:56 PM
As whammy said, use the instr function.



Set fs=Server.CreateObject("Scripting.FileSystemObject")
Set f=fs.OpenTextFile(Server.MapPath("users.txt"), 1)

do while f.AtEndOfStream = false
if InStr(1, f.Readline, "%password'", 1)<>0 Then
Response.Write(f.ReadLine)
Response.Write("<br>")
End If
loop

f.Close
Set f=Nothing
Set fs=Nothing


Also are you wanting to write out the line that comes right after the password line? Because everytime you do f.ReadLine, it reads the next line. So if you wanted to look at the same line mulitple times you'd want to store it in a variable.

whammy
12-14-2002, 12:23 AM
Yeah... what oracleguy said. Usually what I do in that case is add the line (where the text you're looking for exists) to a string variable, with some delimiter like "|" at the end. i.e.:

If InStr(1,f.ReadLine,"blah") Then stringname = stringname & f.ReadLine & "|"

http://www.asp-help.com/getstarted/ms/vbscript/95.asp

Then after you're done reading the file, you can get rid of the last "|" and split (using Split(stringname,"|")) the string into an array (or just use UBound(arrayname)-1 when you loop through it if you don't feel like removing the last delimiter).

Hope this helps. :D