Link to home
Start Free TrialLog in
Avatar of KingSencat
KingSencat

asked on

Retrieve the last 1000 characters from a .txt file using Visual Basic v6.0

I need to retrieve the last 1000 characters(Not lines) from a .txt file, how can i do that with visualbasic ?
Avatar of zoofan
zoofan
Flag of United States of America image

well this function loads the whole text file into a continious string, from that do a right(str,1000)


strLast1000 = right(fileload(strMyFile),1000)


zf



*note you may also want to replace(vbcrlf, "") first to remove end of line

strLast1000 = Right(Replace(FileLoad(strMyFile), vbCrLf, ""), 1000)
Function FileLoad(ByVal sFileName As String) As String
    Dim iFileNum As Integer, lFileLen As Long
 
    On Error GoTo ErrFinish
    'Open File
    iFileNum = FreeFile
    'Read file
    Open sFileName For Binary Access Read As #iFileNum
    lFileLen = LOF(iFileNum)
    'Create output buffer
    FileLoad = String(lFileLen, " ")
    'Read contents of file
    Get iFileNum, 1, FileLoad
 
ErrFinish:
    Close #iFileNum
    On Error GoTo 0
End Function

Open in new window

Avatar of KingSencat
KingSencat

ASKER

I need to return those 1000 characters back to my visual basic project
ASKER CERTIFIED SOLUTION
Avatar of zoofan
zoofan
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Am I missing something?


zf
its show blank, i have  attached the  text document for you, any idea why is not working?
Game10.txt
Wow lol thats some file.  My eyes hurt.  Ok are you using the first or second retrevial?  
This one
strLast1000 = right(fileload(strMyFile),1000)

or this one
strLast1000 = Right(Replace(FileLoad(strMyFile), vbCrLf, ""), 1000)



zf
And now that I have you rfile I will test here as well.  In addition to which retreval you rusing are you trying to place into a variable? textbox? other


zf
I want just to create a new file with the last 1000 characters :) thanks for your help
No problem, you have it working now?

zf
No :( its just blank
what i am doing wrong?
Hi KingSenCat,
I am truly sorry for not getting back sooner.  Work went south in a hurry today and turned into the second Monday of the week.  Got home about 30 min ago and am going to bed.

I will readdress this with you tomorrow and help you figure out whats going on.  First glance I think its the data in the file not being true text formatting not sure but we will get it worked out.



zf