PDA

View Full Version : Counting files in a directory?



Nick
November 18th, 2000, 17:50
Ok, here's the deal I'm making site that has multiple pages and I want to use either a PHP or CGI script to count the number of files in the directory, so I can use a SSI or PHP call to include something like this on the front page:

example: 'We currently have ### articles'

How can I do this?

Thanks in advance.

Grant
November 18th, 2000, 20:33
This should work fine.


#!/usr/bin/perl

# Path to directory you want to count files in on your server.
$dir = "/usr/home/gwscripts/html/files";

$directory_count = 0;
$file_count=0;

print "Content-Type: text/html\n\n";

opendir(DIR, $dir);
LINE: while($FILE = readdir(DIR)) {
next LINE if($FILE =~ /^\.\.?/);

## check to see if it is a directory
if(-d "$FILE"){
$directory_count++;
}
else {
$file_count++;
}
}
closedir(DIR);

print "Directories: $directory_count<br>\n";
print "Files: $file_count\n";

exit;


Hope that helps.

Grant

KapTinKiRk
November 18th, 2000, 21:51
I nominate Grant to be Moderator of this forum, even though he has no time.

Oh, and BTW Grant, I finally ordered that Perl book you recommended, got it today :-)... I already wrote a script (without it) and now I'm adding more stuff to it.

Grant
November 19th, 2000, 03:53
Originally posted by KapTinKiRk
I nominate Grant to be Moderator of this forum, even though he has no time.

Oh, and BTW Grant, I finally ordered that Perl book you recommended, got it today :-)... I already wrote a script (without it) and now I'm adding more stuff to it. What book did you end up getting. I don't remember which one I recommended. It was probably either 'Learning Perl' or 'Programming Perl'. Glad you are learning perl. Let me know if you have any questions with perl.

Nick
November 19th, 2000, 09:35
Thanks for the help Grant. I appreciate it.

KapTinKiRk
November 19th, 2000, 10:59
Originally posted by Grant
What book did you end up getting. I don't remember which one I recommended. It was probably either 'Learning Perl' or 'Programming Perl'. Glad you are learning perl. Let me know if you have any questions with perl.

It was Programming Perl, 3rd Edition

Grant
November 19th, 2000, 23:43
Originally posted by KapTinKiRk
It was Programming Perl, 3rd Edition Ok. That is a great book for learning all the small details of perl.

Grant

Grant
November 19th, 2000, 23:46
Originally posted by Nick
Thanks for the help Grant. I appreciate it. Glad to help. Let me know if you have any other questions.