Page 1 of 2

Copy the Last Line of a .txt file

Posted: Sat Oct 07, 2023 12:30 am
by trags3
I want to be able to copythe last line in a txt file. The file can get very long so I don't want to scroll through the entire file.
I have tried several things without success.
Tom

Re: Copy the Last Line of a .txt file

Posted: Sat Oct 07, 2023 5:27 am
by FourthWorld
Copy to the clipboard? To the end of the file? For display in the UI?

How big is the file?

Re: Copy the Last Line of a .txt file

Posted: Sat Oct 07, 2023 6:30 am
by stam
trags3 wrote:
Sat Oct 07, 2023 12:30 am
I want to be able to copythe last line in a txt file. The file can get very long so I don't want to scroll through the entire file.
I have tried several things without success.
Tom
Perhaps post what you’ve tried and people may be able to help?
I mean it’s not hard to

Code: Select all

set the clipboard to line -1 of myTextVariable
so one would presume that’s not what you mean…

Re: Copy the Last Line of a .txt file

Posted: Sat Oct 07, 2023 5:11 pm
by dunbarx
Stam.

The OP's LC subject history is at a "higher" level, than, say, mine, which is down in the mud. I bet that he did not know one could directly access the last line in a body of text in the way you pointed out.

I may be wrong. If so then like you I wonder if we are grokking his issue.

Tom?

Craig

Re: Copy the Last Line of a .txt file

Posted: Sat Oct 07, 2023 5:33 pm
by FourthWorld
stam wrote:
Sat Oct 07, 2023 6:30 am
I mean it’s not hard to

Code: Select all

set the clipboard to line -1 of myTextVariable
so one would presume that’s not what you mean…
It's only easy when you already know how.

Being able to use negative numbers to count backwards from the implied end of a string of powerful, but not obvious.

And he'd still need to get the file contents onto the variable.

While pop culture most commonly refers to Dunning-Kruger Syndrome as a way of making fun of people who presume expertise without knowing what constitutes expertise, the study was a serious work and has a second half in the outcomes: some who have expertise forget how hard it was for them to acquire it, and thus underestimate the difficulty for newcomers in acquiring it.

I'm guilty of that myself now and then.

Many threads here are filled with guessing replies and little guidance from the OP.

Perhaps we can let trags3 answer the questions I posted to better understand the details of what's needed and how earlier attempts didn't work out.

Re: Copy the Last Line of a .txt file

Posted: Sat Oct 07, 2023 6:00 pm
by trags3
Hi Sorry I wasn't clear re what I am trying to do.
I am working on an APP that will track when different products are built & when distributed distributed to multiple customers. These (edible) products are made up with items that have Lot Numbers that the FDA requires be rigorously tracked. The current process uses pen and paper to record all the required data.
Some of the items going into product are built with the same lot number for as long as several months.
I have a card that allows the user to select any of a number of items and fill out the Lot Number , Date, Quantity etc when the product is packaged and then another card when that product is combined with other products and delivered to a customer (retail).
At each step I save the data in a CSV text file (tLine5.txt) one line per record. There are 8 items per line. I want the data from the previous build to populate the card with items that don't change frequently such as the lot number.
To accomplish this I want to read the last line in the appropriate file and populate the card with the data.
I don't want to scroll thru the entire file to get to the last line if possible.
I can't figure out how to get to the last line in the file without starting at the first and scrolling until I get to the EOF.
Thank You for your help!
Tom

Re: Copy the Last Line of a .txt file

Posted: Sat Oct 07, 2023 6:10 pm
by jiml
I don't want to scroll thru the entire file to get to the last line if possible.
I can't figure out how to get to the last line in the file without starting at the first and scrolling until I get to the EOF.

Code: Select all

function getLastline myFilePath
   open  file myFilePath for read
   -- read a chunk from the end of the file that's large enough to include the file's last line 
   read from file myFilePath at -1000 for 1000 
   close file (fld cFile)
   return line -1 of it
end getLastline

Re: Copy the Last Line of a .txt file

Posted: Sat Oct 07, 2023 6:24 pm
by jiml
At each step I save the data in a CSV text file (tLine5.txt) one line per record.
...
To accomplish this I want to read the last line in the appropriate file and populate the card with the data.
I don't want to scroll thru the entire file to get to the last line if possible.
Alternately, You might consider reversing the line order when you create the CSV file so that the last line is first. In that way you only need to READ line 1 to get the data you need.

Jim Lambert

Re: Copy the Last Line of a .txt file

Posted: Sat Oct 07, 2023 7:08 pm
by dunbarx
Tom.

Whatever the mechanics of reading your file, do you see the adorable method of securing the last line in a dataSet of lines? You instantly see that the first line is logical and straightforward. With a little mental practice, so are the lines at the end.

This is a great tool to know. To get the third to last line, use "line - 3 of..." It is simply the inverse of what is more commonly used, "line 3 of..." Our brains just need a bit of regrooving.

Of course, any chunk can work the same way, characters, words, items, paragraphs...

One cannot do this with controls, as opposed to chunks. Though one can "select btn 3", one cannot "select btn -3". But since LC firstly interprets this sort of thing as layer order, I do not really see why not.

Craig

Re: Copy the Last Line of a .txt file

Posted: Sat Oct 07, 2023 8:36 pm
by richmond62
the adorable method
"adorable" . . . gies me the boak.
-
Screen Shot 2023-10-07 at 22.32.13.png
-

Code: Select all

put fld "rezult"  into REZULT
   put the last line of REZULT into fld "lastLine"

Re: Copy the Last Line of a .txt file

Posted: Sat Oct 07, 2023 8:48 pm
by stam
In an attempt to clarify and simply:
You don't need to read the file line by line as you may do with other languages, neither is any 'scrolling' of lines and checking for EOF required.
You can read the file directly into a variable that will hold the whole file.

You can access the last line by either using:
line -1 - the negative reverses the direction, so -1 is the last line, not the first, very handy for any kind of container/list where item -1 is the last item/line/char etc. Not many (if any) other languages have this convenience.
the last line - self explanatory

While you could do it other ways, this is plenty fast and will likely save you headaches.

in other words you could create a function

Code: Select all

function getLastLineOfFile pFile        #  pFile is the file path to your text file
   local tText 
   put URL ("file:" & pFile) into tText
   return line -1 of tText  -- or alternatively: return the last line of tText -- it's exactly the same
end function getLastLineOfFile

Re: Copy the Last Line of a .txt file

Posted: Sat Oct 07, 2023 8:50 pm
by richmond62
You can read the file directly into a variable that will hold the whole file.
Of course you can, and it is a whole lot more sensible than bunging the text in a field: the reason I did that in my stack was just for illustrative purposes, so that one can check that one HAS the last line.

Re: Copy the Last Line of a .txt file

Posted: Sat Oct 07, 2023 8:53 pm
by stam
Richmond I wasn't referencing your answer - your answer is correct.

I'm referencing the fact that in other languages you would open a text file as a text input stream and either readAll or readLine until EOF (end of file).
It's worth learning about other languages if only to see how good we have it in LC ;)

Re: Copy the Last Line of a .txt file

Posted: Sat Oct 07, 2023 9:09 pm
by richmond62
I was reading in text files with PASCAL V in 1984, so am well aware of the tedium.

My computing project at Durham was to make a concordance of the English translation of Leibniz's Monodology with multi-dimensional arrays. It brought the University computer to a stand-still.

Re: Copy the Last Line of a .txt file

Posted: Tue Mar 11, 2025 8:48 pm
by trags3
Hi Richmond,
I was searching for something else and ran across this post of yours.
It reminded me of the time A buddy of mine and I wrote a program in Fortran to find a Knight's tour of the chessboard.
The IBM 360 that we had access to (in the days of Punch Cards) timed out at 1 minute before a solution was found.
A buddy converted the program to Basic and ran it on a timeshare computer. His terminal was in Okla City, and the computer was somewhere in Ohio.
The answer was spit out after more than 20 minutes.
How things have changed!

Tom Ragsdale