Ask Experts Questions for FREE Help !
Ask
    panapka's Avatar
    panapka Posts: 1, Reputation: 1
    New Member
     
    #1

    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

    Regards,
    Pankaj
    bobbypoppy's Avatar
    bobbypoppy Posts: 1, Reputation: 1
    New Member
     
    #2

    Jan 19, 2011, 04:11 AM
    #!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";
    }



Not your question? Ask your question View similar questions

 

Question Tools Search this Question
Search this Question:

Advanced Search

Add your answer here.


Check out some similar questions!

Need help with Perl! [ 3 Answers ]

I am currently learning how to use Perl from a book. One of the exercises on chapter 4 (Scalars, Arrays and Hashes) has really left me dumb. This is the problem, but I can't figure out the coding to make the "Course code" typed in the user to appear in the place of "Shell Programming" where it...

Perl necklace [ 3 Answers ]

I was given a perl necklace but it is broken And I want to fix it but I can't get the thread out it looks like that there is a knot or something on the thread in the perl How do I get the perls off thr thread? Please help


View more questions Search