Current location: Hot Scripts Forums » Programming Languages » PHP » Please help me to count file *.txt only inside folder+ subfolder


Please help me to count file *.txt only inside folder+ subfolder

Reply
  #1 (permalink)  
Old 07-23-03, 03:01 AM
kevin kevin is offline
Newbie Coder
 
Join Date: Jun 2003
Location: USA
Posts: 29
Thanks: 0
Thanked 0 Times in 0 Posts
Please help me to count file *.txt only inside folder+ subfolder

Hello !

I hope someone can help me to write the PHP code that can count total .txt files inside folder "data" and any subfolder "data"

Thanks for all the help

Kev
Reply With Quote
  #2 (permalink)  
Old 07-24-03, 11:11 AM
ChristGuy ChristGuy is offline
Operations Support Develo
 
Join Date: Jun 2003
Location: Rivonia, South Africa
Posts: 111
Thanks: 0
Thanked 0 Times in 0 Posts
Greetingz...

I'll guess you're looking for something like this:

PHP Code:

<?php

  
function CountDir($aDir$aRecurse)
  {
    
$Count 0;

    
$d dir($aDir);

    while (
$Entry $d->Read())
    {
      if (!((
$Entry == "..") || ($Entry == ".")))
      {
        if (
Is_Dir($aDir '/' $Entry))
        {
          if (
$aRecurse)
          {
            
$Count += CountDir($aDir '/' $Entry$aRecurse);
          }
        }
        else
        {
          
$Parts explode("."$Entry);
          if (
pop($Parts) == "txt")
          {
            
$Count++;
          }
        }
      }
    }
    
    return 
$Count;
  }
?>
Usage:

PHP Code:

echo CountDir($_SERVER["DOCUMENT_ROOT"], True); // Echo's 9


  
echo CountDir($_SERVER["DOCUMENT_ROOT"], False); // Echo's 3 
hope that helps...
__________________
Till We Meet Again...
Clifford W. Hansen
Aspivia (Pty) Ltd

"We Have Seen Strange Things Today!" Luke 5:26
Reply With Quote
  #3 (permalink)  
Old 07-25-03, 12:49 PM
kevin kevin is offline
Newbie Coder
 
Join Date: Jun 2003
Location: USA
Posts: 29
Thanks: 0
Thanked 0 Times in 0 Posts
Hi ChristGuy !

THanks so much for your wonderful code

Anyway, I've modified a bit to make it work right for me (pop function is not supportted on my server)

Take a look at my modification below and let me know what you think:

Code:
<?php
  function CountDir($aDir, $aRecurse)
  {
    $Count = 0;

    $d = dir($aDir);

    while ($Entry = $d->Read())
    {
      if (!(($Entry == "..") || ($Entry == ".")))
      {
        if (Is_Dir($aDir . '/' . $Entry))
        {
          if ($aRecurse)
          {
            $Count += CountDir($aDir . '/' . $Entry, $aRecurse);
          }
        }
        else
        {
#        $Parts = explode(".", $Entry); // Comment this line
#        if (pop($Parts) == "txt") // Replace this code 
         if (preg_match ("/.txt/i",$Entry))
          {
            $Count++;
          }
        }
      }
    }
    
    return $Count;
  }

echo CountDir($_SERVER["DOCUMENT_ROOT"], True); // Echo's 9

?>
How you can add color to your code ChristGuy - Your code is very easy to read on this forum

Have a nice weekend !
Kev.
Reply With Quote
  #4 (permalink)  
Old 07-25-03, 02:22 PM
ChristGuy ChristGuy is offline
Operations Support Develo
 
Join Date: Jun 2003
Location: Rivonia, South Africa
Posts: 111
Thanks: 0
Thanked 0 Times in 0 Posts
Greetingz...

Firstly to add color [ php ] your code here... [ / php ] (With out the spaces

Secondly...
PHP Code:

  preg_match "/\d+\.txt/i"$Entry 
I've tested the above and it works for all file names ending with .txt

Glad it helps...
__________________
Till We Meet Again...
Clifford W. Hansen
Aspivia (Pty) Ltd

"We Have Seen Strange Things Today!" Luke 5:26
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
edit php in Squirrelmail jrcortrightiii PHP 1 07-10-03 02:08 PM
How to count total files inside folder + subfolder? kevin PHP 1 06-22-03 12:50 PM


All times are GMT -5. The time now is 05:02 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.