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!

Copy the last entry

Status
Not open for further replies.

marshyrob

Technical User
Jan 20, 2004
137
GB
Hello All

I have a script that searches a text file for a username that i enter into an input box. It then finds those matching entries and writes them to another text file.

This works fine. But i end up with lots of entries matching the search criteria. All i want is the last entry that it finds. How can i just choose the last entry to be shown!? I have looked into search from the end of the file but i dont know how to impliment just capturing the one entry and finishing.

Here is my script that works:

'Set Constants
Const ForReading = 1
Const ForWriting = 2

Input = InputBox("Enter the Username to Search For")
strSearch = Input

'Open the TXT file
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile("C:\Audit.txt", ForReading)

Do Until objTextFile.AtEndOfStream
strLine = objTextFile.ReadLine
intUser = InStr(strLine, StrSearch)
If intUser > 0 Then
strNewText = strNewText & strLine & VbCrLf
End If
Loop

objTextFile.Close
Set objTextFile = objFSO.OpenTextFile _
("C:\test.txt", ForWriting)
objTextFile.Write(strNewText)
objTextFile.Close

Any help on this would be appreciated.

Rob
 
read the text file.
load each line into a row in an array
let's say the array has 500 entries
then search the array from the bottom up:
Code:
for i = ubound(array) to 1 step -1
    string_pos = Instr(1, eachline, search_string, 1)
    if string_pos > 0 then 
       found = "yes"
       exit for
    end if
next
this will search the array from the bottom up.
 
Thanks Barny2006

Ill try that and see how i get on!

Rob
 
How can i just choose the last entry to be shown!?
In your working script replace this:
strNewText = strNewText & strLine & VbCrLf
with this:
strNewText = strLine & vbCrLf

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