Ask Me Help Desk

Ask Me Help Desk (https://www.askmehelpdesk.com/forum.php)
-   Perl (https://www.askmehelpdesk.com/forumdisplay.php?f=453)
-   -   Need Perl Code for working in directory (https://www.askmehelpdesk.com/showthread.php?t=448945)

  • Feb 19, 2010, 05:03 AM
    panapka
    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

    Regards,
    Pankaj
  • Jan 19, 2011, 04:11 AM
    bobbypoppy
    #!c:/perl/bin/perl.exe
    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) {
    ++$no_files;
    ++$readable if (-r $file);
    ++$writable if (-w $file);

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

    print <<"END_REPORT";
    Report on $directory

    Number of directories:\t$no_dirs

    Number of files:\t$no_files
    \tReadable:\t$readable
    \tWritable:\t$writable


    File Breakdown:
    END_REPORT

    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.