0

I've looked thru the questions already with the system, but couldn't find the answer to my problem. I want to try a counter in a php recursive function, which looks for and deletes empty folders. I paste below a way to echo non-empties as "–" and empties as "|". It looks ok after all, but in case there's a lot to purge, it all grows into gibberish on the screen. Instead I'd like to see the numbers of folders checked vs deleted. Here's the code compiled so far using StackOverflow too. Any help pls?

function RemoveEmptySubFolders($path) {
    echo "–";
    $empty = true;
    foreach ( glob ( $path . DIRECTORY_SEPARATOR . "*" ) as $file ) {
        if (is_dir ( $file )) {
            if (! RemoveEmptySubFolders ( $file ))
                $empty = false;
        } else {
            $empty = false;
        }
    }
    if ($empty) {
        if (is_dir ( $path )) {
        // echo "Removing $path...<br>";
        rmdir ( $path );
        echo "|";
        }
    }
    return $empty;
}
3
  • 1
    You can probably use references in your function : function RemoveEmptySubFolders($path, &$cnt) : php.net/manual/en/language.references.pass.php Commented Oct 22, 2015 at 6:27
  • Do you want to see the total sum of all deleted vs empty or only the ones by the actual recursion? Commented Oct 22, 2015 at 6:49
  • So please accept the answer which solved your problem. Look at the tour(stackoverflow.com/tour) for the use of SO Commented Oct 22, 2015 at 12:09

2 Answers 2

4

Just pass the variables as reference:

function recursive($path, &$directories, &$removed) {
    echo "-";
    $directories ++;
    $empty = true;
    foreach ( glob ( $path . DIRECTORY_SEPARATOR . "*" ) as $file ) {
        if (is_dir ( $file )) {
            if (! recursive ( $file, $directories, $removed ))
                $empty = false;
        } else {
            $empty = false;
        }
    }
    if ($empty) {
        if (is_dir ( $path )) {
            $removed++;
            echo "|";
        }
    }
    return $empty;
}

$path = "c:\exampledir";
$directories = 0;
$removed = 0;
recursive($path, $directories, $removed);
echo("<br>$directories, $removed");

You could also use global variables, but that's very ugly, and every time you use a global variable othen than the standard ones, a kitty dies.

Sign up to request clarification or add additional context in comments.

3 Comments

Vote up for the kitties!
Thanx man, it works now! My mistake was failing to pass along with the function itself the incremented vars in 'if (! recursive ( $file, $directories, $removed ))'.
Now inspired I've added &$dash_count for fun: $dash_count++; if($dash_count > 70) { echo "–<br>"; $dash_count = 0; } else echo "–";
0

Object-Oriented variant:

Put everything in a class and add the instance attributes $checked and $deleted, then increment them with a selfmade procedure or (nasty code) just with += 1 or $var ++.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.