$dir="vol2:\\users\\staff";
$bigfile=5000000;
$outfile="vol2:\\users\\staff\\results.txt";

print "Searching for files larger than $bigfile bytes . . .\n\n";
&searchDirs;
print "\r" . " " x 80;
&printResults;


###############
# Subroutines #
###############

sub searchDirs {
    push(@DIRS, $dir);
    while($newdir=pop(@DIRS)) {
        print "\rChecking: ";
        printStatus($newdir);
        opendir(DH, $newdir) || next if (-d "$newdir") || die "Cannot open dir $newdir: $!"; 
        @FILES=readdir DH;
        closedir(DH);
        foreach $file (@FILES) {
            next if ($file =~ /^\.+$/);       # Skip . and ..
            if (-d "$newdir/$file") {
                 push(@DIRS, "$newdir\\$file");
            } else {
         $size=(stat("$newdir\\$file"))[7];
                 if ($size>$bigfile) {
                     push(@FOUND, "$newdir\\$file: $size\n");
                 }
            }
        }
    }
}


sub printResults {
    print "\nSaving results of search to $outfile.\n\n";
    open(FILEHAND,">$outfile");
    print FILEHAND "Results of search for files larger than $bigfile bytes.\n\n";
    foreach $found (@FOUND) {
        print FILEHAND "$found";
        next;
    }
    close(FILEHAND);
}


sub printStatus {
    my($line) = @_;
    $max = 69;
    $length = length($line);
    if ($length > $max) {
        $line = "..." . substr($line,$length-($max-3));
    } else {
        $line = $line . " " x ($max-$length);
    }
    print "$line";
}

