Skip to main content
BreakawayPaul
Inspiring
November 6, 2011
Question

Delete line from text file

  • November 6, 2011
  • 1 reply
  • 2336 views

I have a text file that's generated by an app, and I want to limit the length by deleting the oldest (i.e. first) like whenever the file grows to 100 lines to keep it from getting too huge.

Other than reading the file into a variable, deleting the line, and re-writing the entire file, is there a better way to just delete the first line of the file?

    This topic has been closed for replies.

    1 reply

    Inspiring
    November 10, 2011

    Coldfusion doesn't have any built in log truncation methods.  Seems like this would be a good candidate for switching to a database table, which would give you a lot more flexibility on working with the records.  Other than that, your options are limited to:

    1) reading the whole file into a variable, writing your new records, manipulating the data to remove the first (oldest) records, and writing to the operating system.

    or

    2) Finding some java library that accomplishes (1)

    BreakawayPaul
    Inspiring
    November 11, 2011

    Thanks.  I started off with this in a table in the database, but since we're stuck with Access, I'm trying to leave as much stuff out of the database as possible to keep the file size down.  Since we're also forced to use a neanderthal meathod to copy the database from the test server to the live server, keeping the file size down makes the copy go faster and lessens the likelyhood that the db will get corrupted during the copy (long story).

    Thanks for the advice!

    Inspiring
    November 11, 2011

    Oh, believe me, you don't have to go into any more details about corrupted Access databases on this forum.You know, I totally forgot but starting in CF 8, you can loop over the contents of a file line by line using <cfloop file="">

    http://livedocs.adobe.com/coldfusion/8/htmldocs/help.html?content=Tags_j-l_10.html

    That might get you to where you need to go.