Length - 1 The code reads the text file and creates an array of Strings with one line in each element of the array. Then it copies the first line into one TextBox and the last line into the other. Note that there is no way to find the last line of a text file without reading the whole file in some way.
If your text file is hundreds of megabytes long, you might not want to read it all into memory at the same time. That seems unlikely, but if that is the case, let us know and we can show you how to just read one line at a time. Hi, No miracle solution, you'll have to read the whole file to reach the last line.
First ' lines. Last ' lines. ReadLine Do Until reader. EndOfStream ' replace "previous" last line with "current" read line , ' hence at end of loop lastLine will contain the "real" last line.
ReadLine Loop End Using Typically the second approach is better if the file are large because you don't have to load the whole file in memory at once. I could test a little my previous post was only based on my memory And I remember of File. LastOrDefault Console. NewLine, first, last, watch. Elapsed With my computer for a file of approx words generated via Lorem Ipsum I got a time of 7 milliseconds.
This will work without reading the entire file. Cells intRow, Close objExcel. Office Office Exchange Server. Not an IT pro? Script Center. Sign in. United States English. Ask a question. Quick access. Search related threads. Remove From My Forums. Answered by:. Archived Forums. The Official Scripting Guys Forum! If your answer is "yes," you know what to do. Dive in and help somebody!
If your answer is "no," welcome to our fun little world! We'd recommend that you first head over to the Script Center, get your feet wet, and then come back to either ask or answer questions. We can't be everywhere at once we know—shocking! However, an extension is basically a label: it tells what should be inside the file, but the actual content may still differ from what the label says. You have to look yourself. ReadLine Wscript. Echo strLine f.
Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password. Post as a guest Name. Email Required, but never shown. The Overflow Blog. Stack Gives Back Safety in numbers: crowdsourcing data on nefarious IP addresses.
Featured on Meta. New post summary designs on greatest hits now, everywhere else eventually. For the purposes of our demonstration script, we then echo the value of strLine and close the text file.
Like we said, not very elegant, but it works. And it works reasonably fast, too. For example, create a sample log file with 10, lines, and modify the script so it stores just the value of line in a variable. How would you do that? Change the For Next loop so that it loops from 1 to Then echo the value of line Beauty is in the eye of the beholder, and we find any script that takes only a second or two to complete to be very beautiful indeed.
0コメント