Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Name: Sci-Guy
How would I go about reading the last 10 characters of line 15 in a 20 line .txt file, then save the resultant string in a variable?

What type of programming language ?
for a differt language let me know
But here is the code using vb scriptDim Files 'As Object
Dim stFileName 'As String
Dim StGetString 'As String
Dim stLastTenChar 'As String
Dim iGetStringLen 'As Integer
Dim iCounter 'As IntegerStFileName="c:\ReadMe.txt"
'Replace "C:\ReadMe.txt" With Your File
'Path and NameSet Files = CreateObject "Scripting.FileSystemObject")
If Files.FileExists(stFileName) Then
Set MyTextFile = Files.OpenTextFile(stFileName,1)
End ifiCounter=0
Do Until MyTextFile.AtEndOfStream = True Or iCounter = 20
iCounter=iCounter+1
StGetString=MyTextFile.ReadLine()
If iCounter=15 then
iGetStringLen=Len(stGetString)
stLastTenChar=Mid(StGetString,(iGetStringLen - 9),10)
MsgBox StLastTenChar 'Here is the output
End if
Loop

this line posted wrong it
Set Files = CreateObject "Scripting.FileSystemObject")it should be
Set Files = CreateObject ("Scripting.FileSystemObject")let me know if this works for you ?

kwolbert,
Thanks for your reply. That's just what I needed, although I usually prefer just to be pointed in the right direction so I can try to figure it out myself. I'm just trying to learn VB and find it better if I have to do some work myself. However, being able to look over the code you provided will still be of great benefit to me. Thanks again.
P.S. the language is stated in the title of my post (VB6).

==================================================
SciGuy,Good for you! I get tired of seeing people that want everything done for them but don't want to find out anything (or at least try to) on there own. Naturally this applies more to those out there that want someone to do their homework for them but I still think if the question needs to be asked then one is better off trying to do some of it on their own. I am much the same way. I don't have any information above what has been already provided and I am not sighting anyone for trying to help you in there own way. I just wanted to acknowledge the true scholastic spirit you have and share my admiration for it.
PS. Those out there who still want someone to give you the answer to your homework assignment, please stop and take a lesson from SciGuy here. The rewards far exceed the expedition of being given the answer straight out; not to mention that your professors/instructors can tell when the answers have been given to you.
borelli35

hey borelli,
don't just jump to conclusions that every goddamn programming question is homework!
they may be part of a larger project or the user may be just a beginner. the above question sounds too general to be homework to me. some users are probaly obsessed with the homework rant thingy.
saddam

"some users are probaly obsessed with the homework rant thingy."
Yeah, but that might have to do with the exorbitant number of threads that show up here with problems just copied word for word from a homework assignment. Case in point.
And I second borelli. How in the world can you condemn him of jumping to conclusions when he simply said 'good job'? It's great when somebody else sees the value of trying to work through something yourself. Most homework is there for a reason. Listening to some professor ramble on about one thing or another is fine and dandy, but until you take a crack at it yourself, you haven't learned anything.
Besides, if someone is having a valid problem, how can we be expected to help unless they show what they've accomplished already?

---Sci-Guy---
"I usually prefer just to be pointed in the right direction so I can try to figure it out myself. I'm just trying to learn VB and find it better if I have to do some work myself."
I share Borelli's comments, your above comment is admirable. Hopefully, your comments will be inspirational for students and professionals alike.
To answer your question at hand, there is a string function in VB6 called Right, it returns the specified number of characters from the right side of the string. The syntax I will leave for you to determine but your IDE will probably lay it out for you anyway. Just use a while loop until end of file, reading each line into a temp string, when the counter hits 15... apply the the Right function with a parameter of 10 to the temp string and read it into a string variable of your choice.
--Saddam--
"every goddamn programming question is homework"The only way your above statement is true, is the fact that most professional programmers do their work at home too in order to meet deadlines and out of love for the profession - therefore, the term homework could very well be applied. However, being that it is unlikely this is what you were implying... not all questions are homework related... project related perhaps, but not all threads start with a request from a student in college or otherwise. Be it as it may that this thread started off with a question that could be used both ways, we need not imply homework. You are right, some users are probaly obsessed with the homework rant "thingy"... I am one of those users... after all, I started the thread. If you want to see why it is important for students to do their own work, perhaps you should visit it. I believe it is number 7407.
---Dr. Nick---
I agree with you, "It's great when somebody else sees the value of trying to work through something yourself." I also appreciate the fact that Sci-Guy wants to work through the code on his on, but asks general questions to the point to where he can get an understanding of the concepts involved.IR

Thanks for all your replies. It was not my intention to start a debate on "homework questions". I don't have the advantage of having a professor or teacher to ask for help. I am a disability pensioner trying to learn a bit of programming from books, internet tutorials and the help of the good people on this forum. I kind of feel sorry for those that do ask for their homework to be done for them. They will probably never get a good grasp on the subject because someone else has done their thinking for them. What I have learned since I began has taught me to respect all the hard work you guys have done to get to where you are now. It would be disrespectful of me to ask you to hand me everything on a silver platter.
Again to all, thanks.

sorry about the words i used. but please have pitty on those who hope to learn some thing. i am sure you can tell what is a homework question or not. in this case it is only a specific function.

![]() |
Hiding source code
|
saving BIOS information
|

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |