$outfile = $ENV{HOME}.'/tmp.txt';
do {
print "Please enter a directory path: ";
chomp($ans = <STDIN>);
$path = $ans if( -d $ans);
}until($path ne '');
opendir(ED, "$path") || die "cannot open directory: $!";
$oldestdate = 9999999999999999999999;
$biggestsize = -1;
while($item = readdir(ED)) {
next if(($item eq '.') || ($item eq '..'));
$fullpath = $path.'/'.$item;
if( -f $fullpath ) {
@fileinfo = stat($fullpath);
if($fileinfo[9] < $oldestdate) {
$oldestdate = $fileinfo[9];
$oldestfile = $fullpath;
}
if($fileinfo[7] > $biggestsize) {
$biggestsize = $fileinfo[7];
$biggestfile = $fullpath;
}
}
}
closedir(ED);
open(OUT, ">$outfile") || die "cannot open file: $!";
$oldstring = "The oldest file found is $oldestfile\n";
print OUT $oldstring;
print $oldstring;
$bigstring = "The largets file found is $biggestfile with a size of $biggestsize bytes.\n";
print OUT $bigstring;
print $bigstring;
close(OUT);
chdir($ENV{HOME});
rename ('./tmp.txt', './lab01.output.txt');
exit;