Re: Traverse directory - Find empty directory
| From: | Holografix | Date: | Fri, 29 Feb 2008 11:26:17 +0000 |
| Subject: | Re: Traverse directory - Find empty directory | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-270726@lists.php.net to get a copy of this message | ||
Hi
Thank you for answering my question Jim.
I didn't notice the message get into the list because I get errors when
posting and then I quit to post and try to solve the problem. Your solution
is good and nice. I used scandir() instead of glob() with a recursive
function.
Best regards
holo
"Jim Lucas" <lists@cmsws.com> wrote in message
news:47C701FC.2020202@cmsws.com...
> 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