BalochDude has asked for the wisdom of the Perl Monks concerning the following question:

# I appreciate the way you guys help people here.
# If possible please also briefly explain your code so that i could know what the code actually means.
# Thanks a lot.
Q 1: How can i get the extension of a file ($file)?
e.g. $file could be anything me.jpg, one.exe, two.psd etc.
i only need the extension.
Q2: Sizes and number of files and sub-directories.
Let me explain what i actually need.
Suppose I have a main directory (maindir).
maindir has sub-directories (sub-dirA, sub-dirB, .....)
Every sub-directory could have further subdirecties (sub-subdirA1, sub-subdirA2, sub-subdirB1, ........)
These sub-subdirectories have files in them.
Note: please note that subdirectories cant have files in them. only sub-subdirectories have files.
I need to find: the total number of sub-directories. total number of sub-subdirectories. total number of files in all the sub-subdirectories and total sizes (combined sizes) of all the files.
Q3: I need to build a list of recently uploaded files.
I have all the info like, file name ($filename), user who uploaded the file ($username), time and date ($timendate), sub-subdirectory where the file is uploaded ($subdirectory, $sub-subdir). I need to save this info of $x number of recently uploaded files in record.txt
$x could be any number defined by the programmer in the code.

Q4: And then i need to seperately retrieve info of the uploaded files.
The last 10 uploaded files were abc.txt, tnt.pl,.....

Thanks again.
Ahsan Balouch
  • Comment on Questions related to files and directories

Replies are listed 'Best First'.
Re: Questions related to files and directories
by Zaxo (Archbishop) on May 18, 2004 at 21:13 UTC

    File::Find can do all your directory tree searching. Within your "wanted" routine, you can use file test operators or stat to increment counts and size totals and check their age. File::Basename or File::Spec will help in splitting up path names.

    All those modules are distributed with perl, so you can read their documentation with perldoc.

    After Compline,
    Zaxo

Re: Questions related to files and directories
by Plankton (Vicar) on May 18, 2004 at 21:05 UTC
    A1) read up on fileparse
    $ perldoc File::Basename
    A2) for file size checkout -s in
    $ perldoc -f-T
    A3) to find recently uploaded files
    $ perldoc File::Find
    A4) see A3

    Plankton: 1% Evil, 99% Hot Gas.