To remove the top part of a file you need to rewrite it. You can only shorten a file using truncate(). The underlying reason for this relates to how files are stored on a disk. Anyway here is an efficient way to do it using seek(). The first line of the 'truncated' file will be of random length and incomplete.
my $logfile = 'c:/test.txt'; my $tmpfile = 'c:/tmp.txt'; my $maxsize = 100; my $size = -s $logfile; my $need_to_dump = $size - $maxsize; if ($need_to_dump > 0 ) { open LOG, $logfile or die $!; open TMP, ">$tmpfile" or die $!; seek LOG, $need_to_dump, 0; print TMP $_ while <LOG>; close LOG; close TMP; unlink $logfile or die $!; rename $tmpfile, $logfile or die $!; unlink $tmpfile or warn "Can't unlink $tmpfile $!\n"; }
cheers
tachyon
s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print
In reply to Re: Limit a filesize
by tachyon
in thread Limit a filesize
by Flame
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |