Re: question about # of files in a directory



ZHAO, BING wrote:
> Hi,
> first, I want to thank all who viewed my first question days
> before, especially to those who took time to answer it. It was
> trenmendous encouragement for a beginner perlee like me. Thanks again.
> My question:
> Is there a way to call or maybe get the # of files in a
> directory?
> I am trying to build a storage directory for files which
> automatically empits itself when the # files reaches 50. If there is
> some commands which do that(like whatever COMMAND(directory) ), the
> problem would be solved, I then will be OK to program the rest of perl.
> To be more specific, I have a CGI online page which takes
> uploaded files(press 'upload' on my website, then upload whatever text
> files, uaually DNA sequence files) then I need to modify the files a bit
> then save the files to the STORAGE directory, the directory can't be
> infinitely increasing, so I need to empty it when the time comes. And I
> need to keep track of the # of files in that 'damn' directory.
> Maybe you got better idea about how to store the files to
> the directory? Let me know then.
>

perldoc -f opendir
perldoc -f readdir
perldoc -f closedir
perldoc -f unlink

This should get you started. You can also look into file globs though I
have never preferred them, for whatever reason.

perldoc -f stat
perldoc -f sort

Might also come in handy as presumably you want to remove the oldest,
highest number, etc.

-- UNTESTED --

opendir my $DIRHANDLE, '/path/to/dir' or die "Can't get directory
handle: $!";

my @filelist = grep { $_ ne '.' and $_ ne '..' } readdir $DIRHANDLE;

closedir $DIRHANDLE;

if (@filelist > 50) {
for my $index (50 .. @filelist) {
unlink $filelist[$index] or die "Can't remove file
$filelist[$index]: $!";
}
}

> Thank you all for reading my 'junk'.
>

HTH,

http://danconia.org

> best,
>
> Bing
>
.



Relevant Pages

  • Re: Datei-Upload ergibt immer leere Dateien
    ... Überprüft dein Code $err auf undef und ... upload() Funktion des CGI moduls? ... aus perldoc CGI: ... | complain when you try to use a string as a filehandle. ...
    (de.comp.lang.perl.cgi)
  • Re: removing files in unix or linux using glob?
    ... unlink $file if -f $file; ... perldoc -f unlink ... perldoc perlfunc and search for -f under "Alphabetical Listing ...
    (perl.beginners)
  • Re: Unlink question
    ... Normally, I do go by perldoc but in this case, ... I want to delete the file IN the directory using unlink. ... > Or be root and supply -U to perl so you can use unlink to remove a ...
    (perl.beginners)
  • Re: Unlink question
    ... > Robert wrote: ... >> pass the directory to unlink without success. ... Normally, I do go by perldoc but in this case, ... except scratch my head. ...
    (perl.beginners)
  • Re: removing files in unix or linux using glob?
    ... unlink $file if -f $file; ... perldoc -f unlink ... perldoc perlfunc and search for -f under "Alphabetical Listing ...
    (perl.beginners)