Re: Traverse directory - Find empty directory
| From: | Jim Lucas | Date: | Thu, 28 Feb 2008 18:48:28 +0000 |
| Subject: | Re: Traverse directory - Find empty directory | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-270620@lists.php.net to get a copy of this message | ||
Holografix wrote:
Hi
I'm using Spl RecursiveDirectoryIterator to traverse a directory and have no problem with it but now I'm stuck. I only need to display directories with no files in it.
Can someone help with this?
My current code:
set_time_limit(0);
$files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator('c:\dev'), RecursiveIteratorIterator::SELF_FIRST);
foreach($files as $file) {
if ($files->isDir()) {
echo $file . "<br/>";
}
}
I have
c:\dev
c:\dev\php
c:\dev\php\file1.php
c:\dev\php\file2.php
c:\dev\php\test
c:\dev\php\test\db
I would like to display 'test' because although there is a 'db' folder, there are no files in c:\dev\php\test
Bets regards,
holo
This should do what you are looking to do.
Someone else might find a better way of doing it, but this will work.
<?php
set_time_limit(0);
$path = './';
$files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path), RecursiveIteratorIterator::SELF_FIRST);
foreach($files as $file) {
if ($files->isDir()) {
if ( ( $results = glob($file.'/*') ) !== false ) {
$file_found = false;
foreach ( $results AS $entry ) {
if ( is_file($entry) ) {
$file_found = true;
break;
}
}
if ( !$file_found )
echo $file . " DOES NOT contain files\n";
}
}
}
--
Jim Lucas
"Some men are born to greatness, some achieve greatness,
and some have greatness thrust upon them."
Twelfth Night, Act II, Scene V
by William Shakespeare