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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Last entry in a file

Status
Not open for further replies.

vallan

Technical User
Aug 13, 2002
156
EU
Does anyone know how I can read the last entry in a file? I have a file tat I receive everyday but I want to only be able to report out the last entry or display the last entry as the files is quite large but new entries get written to it on a daily basis.

In such I will like it to display only entry for each current day.

Thanks
 


Code:
Const ForReading = 1

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\backup\wslist.txt", ForReading)

Do Until objFile.AtEndOfStream
    strLine = objFile.ReadLine
Loop
objFile.Close

Wscript.Echo strLine
 
rather

Code:
Const ForReading = 1

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\backup\wslist.txt", ForReading)

Do Until objFile.AtEndOfStream
    strNextLine = objFile.ReadLine
    If Len(strNextLine) > 0 Then
        strLine = strNextLine
    End If
Loop

objFile.Close

Wscript.Echo strLine
 
Thanks

Sorry, what language is this? How do I use it?

Thanks for your help.
 
It's vbscript
just past into notepad and save with extension .vbs
Here made it easier for you.
Just change path to your file
Code:
Const ForReading = 1

'Path to your file
path = "C:\backup\wslist.txt"

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(path, ForReading)

Do Until objFile.AtEndOfStream
    strNextLine = objFile.ReadLine
    If Len(strNextLine) > 0 Then
        strLine = strNextLine
    End If
Loop

objFile.Close

Wscript.Echo strLine
 
Oh my my, it worked, it worked. Thank you so very much. Thanks

Please 2 more helps.

If I want to read the previous 10 lines
If I want to read entries for today's dates e.g Feb 27, the lines begin with Feb 27.

Thanks
 
I found that little exercise a bit more difficult than first expected, for my very limited "programming skills".
Have not looked at the last ten line thing but I'm sure if you post it in vbscript forum someone will help. It will need to have a for loop [just not sure]e.g. i = 1 to 10 or a
Do Until i = 10
i = i -1
If i < 10 Then Exit Do
Loop

As for the date question, here is the code, input is case sensitive. This will echo to screen and write the line to a log file.

Hope it helps

Code:
Const ForReading = 1
Const ForWriting = 2
'Path to your file
path = "C:\backup\wslist.txt"
LogFile = "c:\backup\Found.log"
strSearchString =  Inputbox ("Enter the date")

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(path, ForReading)
				
Do Until objFile.AtEndOfStream
    strLine = objFile.ReadLine
    intStrSearch = InStr(strLine, strSearchString)
    If intStrSearch > 0 Then
        strNewText = strNewText & strLine & vbCrLf
    End If
Loop

objFile.Close
wscript.echo(strNewText)

Set objFile = objFSO.CreateTextFile (logfile, ForWriting)
	objFile.Write(strNewText)
objFile.Close
 
Thank you very much. I will work with and on this. Thank for your help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top