469,336 Members | 5,570 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,336 developers. It's quick & easy.

need to get the newest and oldest files created

4
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
Nov 22 '06 #1
7 5437
GunnarH
83
i have completed most of the requirements
Good. Show us the code you have, and somebody may be able to give you a hand.

2. i need to put it into a gooey
What's a gooey?
Nov 22 '06 #2
florin
4
u want a piece of the code, cause the whole thing is around 600+ lines of code?
Nov 22 '06 #3
GunnarH
83
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.
Nov 22 '06 #4
florin
4
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\\$line")) {
close($file);
}elsif($line ne "." && $line ne "..") {
$count++;
$count +=numberofdir("$key\\$line");
}
}
closedir($handle);
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\\$line")) {
$count++;
close($file);
}elsif($line ne "." && $line ne "..") {
numfiles("$key\\$line");
}
}
$filesinalldir{"$key\\$line"} = $count;
closedir($handle);
}

#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\\$line")) {
$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($handle);

}
#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\\$line")) {
$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($handle);

}
Nov 22 '06 #5
GunnarH
83
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:
Expand|Select|Wrap|Line Numbers
  1. use File::Find;
  2.  
  3. sub lastmodifiedfile {
  4.     my $dir = shift;
  5.     -d $dir or die "'$dir' is not a directory\n";
  6.     my %files;
  7.     File::Find::find (
  8.         sub {
  9.             my $name = $File::Find::name;
  10.             $files{$name} = (stat $name)[9] if -f $name;
  11.         }, $dir
  12.     );
  13.     ( sort { $files{$a} <=> $files{$b} } keys %files )[-1];
  14. }
  15.  
  16. print lastmodifiedfile('/some/directory');
HTH
Nov 22 '06 #6
florin
4
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
Nov 22 '06 #7
GunnarH
83
they don't give me any errors, i just broke it down in pieces.
I was talking about the prototypes.
Expand|Select|Wrap|Line Numbers
  1. sub numberofdir() {
  2. ---------------^^
They prevent you from passing arguments to the subs.
Nov 22 '06 #8

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by Bill H | last post: by
19 posts views Thread by ern | last post: by
6 posts views Thread by daveyand | last post: by
2 posts views Thread by Kidan | last post: by
reply views Thread by | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by Marylou17 | last post: by
1 post views Thread by Marylou17 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.