Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Search a txt file for the last occurrence of a name

Status
Not open for further replies.

marshyrob

Technical User
Jan 20, 2004
137
GB
Hello All

I have a txt file that logs users login attemps everyday.
the line it writes looks like this:

9:56:12 9/3/2006,jbloggs,Lap01234,10.0.0.1

I would like to search this txt file for the last occurrence of a login but im not too sure what to do?

I know i need to use FSO but im stumped as to what the code would be.

Does anyone have a quick example they can point me to to open a txt file search it for the last occurrence of a "jbloggs" and then write that line to another txt file?

Any help would be appreciated im not a VB person but you guys have helped me before.

Thanks in advance

Rob
 
if the entries are time-based, surely the last longon of a user would be towards the end of the file. you can start reading the file from the end and put the first occurence of each user into an array (the latest logon).
then print the array.
each time you come across the same user, check the entry in the array, if it's populated, then bypass it( you have already logged the latest one in the array).
 
here's a script to read a txt file from the bottom up:
Code:
Dim arrFileLines()
i = 0

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\FSO\ScriptLog.txt", 1)

Do Until objFile.AtEndOfStream
     Redim Preserve arrFileLines(i)
     arrFileLines(i) = objFile.ReadLine
     i = i + 1
Loop

objFile.Close

For l = Ubound(arrFileLines) to LBound(arrFileLines) Step -1
    Wscript.Echo arrFileLines(l)
Next
 
barny2006, have a look at the ReadAll method and the Split function.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
cool, phv.
that's a good one. i'm assuming you can "split" the lines with line-feed or something?
 
Dim arrFileLines
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\FSO\ScriptLog.txt", 1)
arrFileLines = Split(objFile.ReadAll, vbCrLf)
objFile.Close
...

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top