News:

SMF 2.1.4 has been released! Take it for a spin! Read more.

Main Menu

script to count files with an extension

Started by Shadow's Pawn, August 03, 2003, 09:41:05 PM

Previous topic - Next topic

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.

$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;



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:<?php 
$files 
= array();
$dir = @opendir("/usr/home/web/users/a0013745/html/RodSerling/Chat/poets"); 
while ((
$file readdir($dir)) !== false) { 
if ( 
substr$file, -) == ".who" ) {
$files[] = "$file,";
    }

closedir($dir);
sort$files );

// Output results
echo count($files);
echo 
" files match";
echo
"</br>";
foreach( 
$files AS $name ) {
echo 
$name;
}
?>


Methonis
Joshua "Methonis" Frazer
Support Specialist
The Simple Machines Team

Metho

#3
Whoops! Didn't see that you didn't want to have the extensions shown, this will take them out:
<?php 
$files 
= array();
$dir = @opendir("/usr/home/web/users/a0013745/html/RodSerling/Chat/poets");
while ((
$file readdir($dir)) !== false) { 
if ( 
substr$file, -) == ".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);
}
?>


I'm thinking preg_replace is probably faster than ereg_replace though...

Methonis
Joshua "Methonis" Frazer
Support Specialist
The Simple Machines Team

[Unknown]

Yes it is..

echo preg_replace ('~\.who~i', ', ', $name);

I think..

-[Unknown]

Metho

Use that instead of my ereg_replace, please. Thanks, [Unknown]. :D

Methonis
Joshua "Methonis" Frazer
Support Specialist
The Simple Machines Team

chris

#6
Take a look at: http://de.php.net/manual/en/function.pathinfo.php and http://de.php.net/manual/en/function.basename.php

untested:

<?php 

   $files 
= array();
   $dir = @opendir('/usr/home/web/users/a0013745/html/RodSerling/Chat/poets');
   while (($file readdir($dir)) !== false)
   {
      $parts   pathinfo($file);
      if ($path_parts['extension'] == '.who')
      {
         $files[] = basename($parts['basename'],'.who');
      }
   
   closedir($dir);
   sort($files);

   // Output results
   echo count($files).' files match<br />';
   foreach($files AS $name)
   {
      echo $name;
   }

?>


;D

Advertisement: