need to get the newest and oldest files created

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • florin
    New Member
    • Nov 2006
    • 4

    need to get the newest and oldest files created

    hi, i have been working on a project that my brother gave me, im supposed to get the statistics of a directory and print them out in a html format, i have completed most of the requirements, but i still have a few problems

    1. i need to get the newest and oldest files created (ex. the date)
    2. i need to put it into a gooey, i guess he doesn't want to use apache, so have it on the desktop

    if u have any ideas on how to do this i would greatly appreciate your help, thanks
  • GunnarH
    New Member
    • Nov 2006
    • 83

    #2
    Originally posted by florin
    i have completed most of the requirements
    Good. Show us the code you have, and somebody may be able to give you a hand.

    Originally posted by florin
    2. i need to put it into a gooey
    What's a gooey?

    Comment

    • florin
      New Member
      • Nov 2006
      • 4

      #3
      u want a piece of the code, cause the whole thing is around 600+ lines of code?

      Comment

      • GunnarH
        New Member
        • Nov 2006
        • 83

        #4
        Originally posted by florin
        u want a piece of the code, cause the whole thing is around 600+ lines of code?
        Yes, please. :)

        Show at least how you grab the contents of the directory.

        Comment

        • florin
          New Member
          • Nov 2006
          • 4

          #5
          ok, ah these are the subroutines, what i did i broke everything down in pieces that way it would be easier for me to work with them and once one subroutine worked i left it alone and focused on the other part here are some subrotines. and if this isn't good i'll just print out the whole file, or code



          #This gets how many directories i have
          sub numberofdir() {

          my $key = shift;
          my $handle;
          my $line;
          my $file;
          my $count;

          opendir($handle , $key) or return;

          while($line = readdir($handle )) {
          if (open($file, "$key\\$lin e")) {
          close($file);
          }elsif($line ne "." && $line ne "..") {
          $count++;
          $count +=numberofdir(" $key\\$line");
          }
          }
          closedir($handl e);
          return $count;
          }

          # number of files in all directories
          sub numfiles() {

          my $key = shift;
          my $handle;
          my $line;
          my $count;
          my $file;

          opendir($handle , $key) or return;

          while($line = readdir($handle )) {
          if(open($file, "$key\\$lin e")) {
          $count++;
          close($file);
          }elsif($line ne "." && $line ne "..") {
          numfiles("$key\ \$line");
          }
          }
          $filesinalldir{ "$key\\$lin e"} = $count;
          closedir($handl e);
          }

          #this gets the smallest directory size
          sub smallfilecount( ) {

          my @filecount;
          $filecount[0] = 30;
          my $keys;

          foreach $keys ( keys %filesinalldir) {
          if ($filesinalldir {$keys} < $filecount[0]) {
          $filecount[0] = $filesinalldir{ $keys};
          $filecount[1] = $keys;
          }
          }
          return @filecount;
          }

          sub largefilecount( ) {

          my @filecount;
          $filecount[0] = 30;
          my $keys;

          foreach $keys ( keys %filesinalldir) {
          if ($filesinalldir {$keys} > $filecount[0]) {
          $filecount[0] = $filesinalldir{ $keys};
          $filecount[1] = $keys;
          }
          }
          return @filecount;
          }

          #gets the sizes of the dir by adding all the files together
          sub sizeofdir() {

          my $key = shift;
          my $file;
          my $line;
          my $handle;
          my $filesize;
          my @sizefile;
          my $sum;
          my $keys;

          opendir($handle , $key) or return;

          while($line = readdir($handle )) {
          if(open($file, "$key\\$lin e")) {
          $filesize = -s("$key\\$line" );
          push @sizefile, $filesize;
          close($file);
          }elsif ($line ne "." && $line ne "..") {
          sizeofdir("$key \\$line");
          }
          }
          foreach $keys ( @sizefile ) {
          $sum += $keys;
          }

          $dirsize{$key} = $sum;
          closedir($handl e);

          }
          #gets the sizes of the dir by adding all the files together
          sub sizeofdir() {

          my $key = shift;
          my $file;
          my $line;
          my $handle;
          my $filesize;
          my @sizefile;
          my $sum;
          my $keys;

          opendir($handle , $key) or return;

          while($line = readdir($handle )) {
          if(open($file, "$key\\$lin e")) {
          $filesize = -s("$key\\$line" );
          push @sizefile, $filesize;
          close($file);
          }elsif ($line ne "." && $line ne "..") {
          sizeofdir("$key \\$line");
          }
          }
          foreach $keys ( @sizefile ) {
          $sum += $keys;
          }

          $dirsize{$key} = $sum;
          closedir($handl e);

          }

          Comment

          • GunnarH
            New Member
            • Nov 2006
            • 83

            #6
            Why the prototypes? Those subs don't do anything but giving you fatal errors, do they?

            Anyway, there are modules to traverse directory trees, e.g. File::Find. This example returns the name of the last modified file:
            Code:
            use File::Find;
            
            sub lastmodifiedfile {
            	my $dir = shift;
            	-d $dir or die "'$dir' is not a directory\n";
            	my %files;
            	File::Find::find (
            		sub {
            			my $name = $File::Find::name;
            			$files{$name} = (stat $name)[9] if -f $name;
            		}, $dir
            	);
            	( sort { $files{$a} <=> $files{$b} } keys %files )[-1];
            }
            
            print lastmodifiedfile('/some/directory');
            HTH

            Comment

            • florin
              New Member
              • Nov 2006
              • 4

              #7
              they don't give me any errors, i just broke it down in pieces. But thanks for the help im gonna put it in my script and see how it goes, thanks again

              Comment

              • GunnarH
                New Member
                • Nov 2006
                • 83

                #8
                Originally posted by florin
                they don't give me any errors, i just broke it down in pieces.
                I was talking about the prototypes.
                Code:
                sub numberofdir() {
                ---------------^^
                They prevent you from passing arguments to the subs.

                Comment

                Working...