PDA

View Full Version : Script to show files in directory


carlosbanderos
12-10-2005, 11:39 PM
Hey guys,

I need a short script to put in a html file that will allow me to show how many files are in a particular directory. Its only the number I`m after. should be quite simple yet I`ve spent hours searching to no avail. I guess it needs to be cgi but could also do php or even do a count of mysql database files as that will also be the same. However I have no idea how to do any of these ...!!

Any ideas ? (I use apache)

cheers

Carl

JUD
12-15-2005, 01:29 AM
Hi carlosbanderos

Welcome to the forums

This simple little script should do what you're after


#!/usr/bin/perl

use CGI qw(:standard);
use CGI::Carp qw(warningsToBrowser fatalsToBrowser);

print header();

my $dir = "path/to/your/directory";
my $i = 0;

opendir(DIR, $dir);
@files = readdir(DIR);
foreach(@files){
if($_ !~ m/^\.{1,2}$/){
$i++;
}
}
closedir(DIR);

print "There are $i files in $dir";


Just replace 'path/to/your/directory' with the directory you want to count

JUD :thumbsup:

FishMonger
12-15-2005, 02:08 AM
Here's a more condensed and efficient version.
#!/usr/bin/perl -w

use strict;
use CGI qw(:standard);

my $dir = 'path/to/your/directory';
my $i;

print header();

while(<$dir/*>){$i++}
print "There are $i files in $dir";