PDA

View Full Version : File counting script



Gayowulf
December 4th, 2001, 12:57
Is there a way to have all files of a certain type counted up, and that number displayed on a different page?

It sounds possible, but I'm not sure.

Dusty
December 4th, 2001, 15:30
This would count the number of HTML file in whatever directories you specify and print the number out. If you wanted it printed out on another page, just call the script from it with SSI. Edit the directories in the @dirs array to change where you want the script to look, change ".html" to whatever extension it is you want to look for.
#!/usr/bin/perl

@dirs=("/path/to/1st/dir","/path/to/2nd/dir","/path/to/3rd/dir");
$num=0;
foreach $dir(@dirs){
opendir(DIR,$dir);
foreach $file(readdir(DIR)){
$num++ if $file=~/\.html$/i;
}
closedir(DIR);
}
print "Content-type: text/html\n\n";
print "There are ".$num." HTML files";

Dusty
December 4th, 2001, 15:34
...or if you insist on PHP:
<?php

$dirs=array("/path/to/1st/dir","/path/to/2nd/dir","/path/to/3rd/dir");
$num=0;
foreach($dirs as $dir){
$dir=opendir($dir);
while($file=readdir($dir)){
if(preg_match("/\.html$/i",$file)){
$num++;
}
}
closedir($dir);
}
print "There are ".$num." HTML files";

?>