Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Count the number of files that are recursively searched for.

Status
Not open for further replies.

chrismassey

Programmer
Aug 24, 2007
264
GB
Hello,

I have a function which I use for displaying the entire contents of a directory (a recursive process). It works great, but the only problem is I also use it for a search process (to search for a particular file) and at the end of the search I want to display how many directories were found and how many files were found. I basically do this by adding 1 to $dir_count for a directory and $file_count for a file. I then return these results so that they can be displayed at the end.

However, I am not totally sure why, but I think because of the recursive nature of the function, the result is not returned once at the very end, but returned many times (for example if there are 3 levels of directories, then the result will be returned 3 times) and inevitably the result displayed is the result of the final return.

Does anyone have any suggestions how I can improve my code so that it returns an accurate result of how many directories/files have been found?

Thank you very much,

Chris

(I cleaned the code up so only code regarding search remains):
Code:
function overview($main_action, $script_url, $overview_path, $ii_url, $p_tb1, $p_sb2, $dir_count, $file_count, $i_gap_count, $file_carry, $cm_carry, $cm_action, $dir_path) {
	$path = $overview_path.'/'.$dir_path;
	$url = $ii_url.'/'.$dir_path;
	$handle = opendir($path);
	while($file = readdir($handle)) {
		if ($file == '.' || $file == '..') {
			continue;
		}
		$new_dir_path = $dir_path.'/'.$file;
		$new_path = $path.'/'.$file;
		$new_url = $url.'/'.$file;
		if(is_dir($new_path)) {
			if (stristr($file, $p_tb1) === FALSE) { }
			else { 
				echo "<img src=\"Images/Directory.bmp\" title=\"Directory\"> <a href=\"$script_url?path_carry=c&file_carry=$file_carry&cm_carry=$cm_carry&cm_action=$cm_action\" title=\"$new_dir_path\"><b>$file</b></a><br>"; 
				$dir_count++; 
			}
			overview($main_action, $script_url, $overview_path, $ii_url, $p_tb1, $p_sb2, $dir_count, $file_count, $i_gap_count, $file_carry, $cm_carry, $cm_action, $new_dir_path);
		}
		else {
			if (stristr($file, $p_tb1) === FALSE) { }
			else { 
				echo "<img src=\"Images/File.bmp\" title=\"File\"> <a href=\"$new_url\" target=\"_blank\" title=\"$new_dir_path\">$file</a><br>";
				$file_count++; 
			}
		}
	}
	closedir($handle);
	$return = "$dir_count|$file_count";
	return $return;
}
 
Well if the function works to display data, then it's good.
Problem is that in a recursive function when you return a value in an recursion step it usualy be returned each time you will leave that step so if you have a folder tree with 3 depths you will have that value 3 times, so to avoid that try to use referenced variable into your function call for example use $dir_count, $file_count as references and dont return any value in the function

Code:
$dir_count=0;
$file_count=0;
overview(... &$dir_count, &$file_count...)
$count = "$dir_count|$file_count";

//and you take out this code from function also, use reference on $dir_count and $file_count variables inside.
//    $return = "$dir_count|$file_count";
//    return $return;

________
George, M
Searches(faq333-4906),Carts(faq333-4911)
 
Nice one, thanks alot,

It worked perfectly and I could shorten parts of my code because of using references too. Thanks again,

Chris
 
you can also use the 'static' keyword which then would not clog up your namespace.
glob() may be another potential solution.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top