Jump to content

Simple count files


birman

Recommended Posts

Hi

How can I do count all files within all directories contained in one directory?

Example:

 

/root

/file1.txt

-dir1/file1.jpg

-dir1/dir_2/file5.png, others.doc

-dir1/dir_2/otherfile.php

........

 

Total: 5 files

 

Thanks

Link to comment
Share on other sites

Got bored, so i put this together:

 

 

<?php
function no_of_files($start_dir,$sub_dir=FALSE){
	$no_of_files=0;
	if($sub_dir === false){
	$handler = opendir($start_dir);	
}else{
 	$start_dir = $start_dir.$sub_dir;
	$handler = opendir($start_dir);
}

while(false !== ($file = readdir($handler))){
	if($file != '.' && $file != '..'){
		if(is_dir($start_dir.'\\'.$file)){//if this is a directory, recall the function with the subdirectory defined
			$dir_name = '\\'.$file;
			$no_of_files += no_of_files($start_dir,$dir_name);	
		}else{//is a file, so increase our counter
			$no_of_files++;	
		}	
	}
}
return $no_of_files;
}
echo no_of_files('C:\Documents and Settings\Ben\My Documents\My Music');
?>

 

Tested and compared with the number of files reported by windows. Seems to work fine to me.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.