General Community > Scripting Help
script to count files with an extension
Shadow's Pawn:
I'm using PfaBB at the moment for one of my sites and I tried this code in a block..
This is supposed to count the number of files with the extension .who in a directory. The absolute path is correct.
--- Code: ---$chat_room_dir = '/usr/home/web/users/a0013745/html/RodSerling/Chat/poets';
$chat_dir = dir($chat_room_dir);
$files = array();
while ($entry = $chat_dir->read())
{
if (substr($entry, 0, 3) == '.who')
$files[] = $entry;
}
$usercount = count($files);
echo $usercount;
--- End code ---
Any ideas what I am doing wrong?
Also, I know this is possible but I have no idea how to do it.. Can someone show me how to list the .who files in this format file1, file2, file3,? But I don't want it to show the .who extension when it is listed.
Any help would be much appreciated!
Spaceman-Spiff:
substr($entry, 0, 3) will return the 1st 3 characters
if u want the last 4 characters, use: substr($entry, -4)
more details: http://www.php.net/substr
Metho:
This should work:
--- Code: ---<?php
$files = array();
$dir = @opendir("/usr/home/web/users/a0013745/html/RodSerling/Chat/poets");
while (($file = readdir($dir)) !== false) {
if ( substr( $file, -4 ) == ".who" ) {
$files[] = "$file,";
}
}
closedir($dir);
sort( $files );
// Output results
echo count($files);
echo " files match";
echo"</br>";
foreach( $files AS $name ) {
echo $name;
}
?>
--- End code ---
Methonis
Metho:
Whoops! Didn't see that you didn't want to have the extensions shown, this will take them out:
--- Code: ---<?php
$files = array();
$dir = @opendir("/usr/home/web/users/a0013745/html/RodSerling/Chat/poets");
while (($file = readdir($dir)) !== false) {
if ( substr( $file, -4 ) == ".who" ) {
$files[] = $file;
}
}
closedir($dir);
sort( $files );
// Output results
echo count($files);
echo " files match";
echo"</br>";
foreach( $files AS $name ) {
echo ereg_replace (".who", ", ", $name);
}
?>
--- End code ---
I'm thinking preg_replace is probably faster than ereg_replace though...
Methonis
[Unknown]:
Yes it is..
echo preg_replace ('~\.who~i', ', ', $name);
I think..
-[Unknown]
Navigation
[0] Message Index
[#] Next page
Go to full version