Closed Thread
Results 1 to 5 of 5

Thread: Number of files and directories in folder

  1. #1
    dirkfirst is offline Programming Expert
    Join Date
    May 2006
    Posts
    354
    Rep Power
    23

    Number of files and directories in folder

    How can I find the number of iles and directories and folder? I only want to count certain file type extensions such as .html, .htm, .php etc. Anyone can help me with the code to do this?

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    dirkfirst is offline Programming Expert
    Join Date
    May 2006
    Posts
    354
    Rep Power
    23
    Anyone?

  4. #3
    RobSoftware is offline Programmer
    Join Date
    Nov 2005
    Posts
    143
    Rep Power
    0
    Code:
        $directory = "/"
    
        // create an array to hold directory list
        $results = array();
    
        // create a handler for the directory
        $handler = opendir($directory);
    
        // Create a total file counter
        $totalFiles = 0;
    
        // Create counter for file types
        $myFiles = 0;
    
        // keep going until all files in directory have been read
        while ($file = readdir($handler)) {
    
            // if $file isn't this directory or its parent, 
            // add it to the results array
            if ($file != '.' && $file != '..') {
       
             // Counter for total files
             $totalFiles++;
    
            // Get our extension
            $myExt = substr($file,strlen($file)-3);
      
           // Test our string
           if ($myExt == ".doc" || $myExt == ".txt") { // Replace .doc and .txt here
                  $myFiles++;
            }
          }
       }
    
        // close the handler
        closedir($handler);
    I didn't include directories but you can work that out from the code above.
    Most of this code came from: http://www.laughing-buddha.net/jon/php/dirlist/

  5. #4
    dirkfirst is offline Programming Expert
    Join Date
    May 2006
    Posts
    354
    Rep Power
    23
    Great! Thank you. I'll work out the rest from that. I've actually learned a lot about PHP since I posted this.

  6. #5
    Ronin is offline Programming Professional
    Join Date
    Apr 2006
    Posts
    309
    Rep Power
    24
    Nice scripts, i'm going to save that one for later use.
    Thanks.

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Beginner Assembly, Directory Handling - Removing Directories and Files (Win32, NASM)
    By RhetoricalRuvim in forum Assembly Tutorials
    Replies: 0
    Last Post: 08-27-2011, 04:52 PM
  2. Read all files in a folder
    By Silvestros in forum C and C++
    Replies: 11
    Last Post: 11-15-2010, 03:06 PM
  3. How to skip Protected Directories and Files in a Inventory Scan
    By Sharper_Software in forum Visual Basic Programming
    Replies: 3
    Last Post: 06-12-2010, 09:55 PM
  4. Visual Basic List Files and Directories in Root
    By QuackWare in forum Classes and Code Snippets
    Replies: 0
    Last Post: 01-24-2010, 10:02 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts