• Howdy! Welcome to our community of more than 130.000 members devoted to web hosting. This is a great place to get special offers from web hosts and post your own requests or ads. To start posting sign up here. Cheers! /Peo, FreeWebSpace.net
managed wordpress hosting

Counting files in a directory?

Nick

Well-Known Member
NLC
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.
 
This should work fine.
Code:
#!/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
 
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.
 
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.
 
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
 
Back
Top