Ask Me Help Desk

Ask Me Help Desk (
-   Perl (
-   -   Need Perl Code for working in directory (

  • Feb 19, 2010, 05:03 AM
    Need Perl Code for working in directory
    Hi All,

    I need a perl script for the following :

    Write a script which takes a directory name and provides a series of statistics about that directory. It should say:

    •How many files it contains
    •How many of the files are readable and writable
    •How many subdirectories it contains
    •It should also break down all the files into file types based on the last 3 letters in their name. These should be

    Listed with the most common extensions first.

    If the name provided either doesn’t exist, or isn’t a directory the script should throw a helpful error message
    ++++++++++++++++++++++++++++++++++++++++++++++++++ ++

    Please help me out

    Thanks in advance

  • Jan 19, 2011, 04:11 AM
    use warnings;
    use strict;
    use diagnostics;

    my $directory = "C:/Windows";

    my $no_files = 0;
    my $no_dirs = 0;
    my $writable = 0;
    my $readable = 0;
    my %extensions;

    unless (-e $directory) {
    die "Sorry - '$directory' doesn't exist";

    unless (-d $directory) {
    die "Sorry - '$directory' isn't a directory";

    chdir $directory or die "Can't move to $directory: $!";

    while (my $file = <*>) {
    if (-f $file) {
    ++$readable if (-r $file);
    ++$writable if (-w $file);

    my $ext = lc(substr($file,-3,3));
    elsif (-d $file) {

    print <<"END_REPORT";
    Report on $directory

    Number of directories:\t$no_dirs

    Number of files:\t$no_files

    File Breakdown:

    foreach my $ext (sort {$extensions{$b} <=> $extensions{$a}} keys %extensions) {
    print "\t$ext\t$extensions{$ext}\n";

  • All times are GMT -7. The time now is 12:43 PM.