3

I have a script which goes through every filename in a directory and removes unwanted files. Amount other things, it matches filenames in a CSV file and then removes a file in an adjacent cell.

<?php
$gameList = trim(shell_exec("ls -a -1 -I . -I .. -I Removed"));
$gameArray = explode("\n", $gameList);

shell_exec('mkdir -p Removed');

echo "\033[01;32m\n\n<<<<<<<<<<Starting Scan>>>>>>>>>>\n\n";

// Do the magic for every file
foreach ($gameArray as $thisGame)
{
    // Ensure continue if already removed
    if (!$thisGame) continue;
    if (!file_exists($thisGame)) continue;

    // Manipulate names to look and play nice
    $niceName = trim(preg_replace('%[\(|\[].*%', '', $thisGame));
    $thisGameNoExtension = preg_replace('/\.' . preg_quote(pathinfo($thisGame, PATHINFO_EXTENSION) , '/') . '$/', '', $thisGame);

    // Let the user know what game is being evaluated
    echo "\033[00;35m{$thisGameNoExtension} ... ";

    $f = fopen("ManualRegionDupes.csv", "r");
    while ($row = fgetcsv($f))
    {
        if ($row[1] == $thisGameNoExtension)
        {
            $primaryFile = $row[0];

            $ext = pathinfo($thisGame, PATHINFO_EXTENSION);
            $fCheck = trim(shell_exec("ls -1 " . escapeshellarg($primaryFile) . "." . escapeshellarg($ext) . " 2>/dev/null"));
            if ($fCheck)
            {
                echo "ECHO LION";
                shell_exec("mv " . escapeshellarg($thisGame) . " Removed/");
                continue;
            }
            else
            {
                echo "ECHO ZEBRA";
                continue;
            }

            break;
        }
    }
    fclose($f);

    echo "Scanned and kept";
}
echo "\033[01;32m\n<<<<<<<<<<Process Complete>>>>>>>>>>\n\n";

?>

It's working, however I don't understand why I am seeing the final echo "Scanned and Kept" straight after either "ECHO ZEBRA" or "ECHO LION" - as I have "continue" calls straight after them, which should restart the loop and move on to the next file. I'm sure I just need to re-jig something somewhere, but I've been fiddling for hours and I'm completely stumped. I'd be super greatful for any help! Many thanks!

UnluckyForSome9
  • 301
  • 1
  • 9
  • Because `echo "Scanned and kept";` is outside the while loop you were in. When the `while` finishes, it moves on to complete whats left in the `foreach`. Which is that echo. – IncredibleHat Jul 13 '18 at 17:10
  • Another friendly suggestion: use `unlink` and try to avoid `shell_exec` when built-in options are available (`glob` and `opendir` / `readdir`). – MatsLindh Jul 13 '18 at 17:14
  • 1
    What's the point of putting `break` after the `if`? You'll never get there since both branches contain `continue`. – Barmar Jul 13 '18 at 17:34
  • 1
    @IncredibleHat Which is why I made it a comment. Also, if you continue in both branches of the `if`, you might as well put the continue *after* the `if`. – Barmar Jul 13 '18 at 17:46
  • 1
    @MatsLindh Where is he using `shell_exec()` to remove a file? He's using it to move a file. The proper replacement would be `rename()`. – Barmar Jul 13 '18 at 17:47
  • @Barmar Of course. My bad. – MatsLindh Jul 13 '18 at 18:08

2 Answers2

4

The continue is just working for the inner loop which is reading the individual lines. If you want to skip to the end of the outer loop, you will need to use...

continue 2;

This allows you to say continue for 2 levels of loop.

Nigel Ren
  • 56,122
  • 11
  • 43
  • 55
0

You have nothing but a break after your continue calls, what else would it run? Edit, if you want the continue on the outer loop, move it there. The continue is just in the wrong loop.

Nimantha
  • 6,405
  • 6
  • 28
  • 69
Dan Chase
  • 993
  • 7
  • 18