1

有没有办法排除缺少某些文件的文件夹?

例如,我有如下文件夹:

FolderA
    aaa.php
    bbb.php
    ccc.php

FolderB
    aaa.php
    bbb.php
    ccc.php

FolderC
    aaa.php

FolderD
    aaa.php
    bbb.php
    ccc.php

我只想拥有FolderAFolderBFolderD(或排除FolderC),因为FolderC没有所有预期的文件。

当前来源

$dirs   = [];
$finder = new Finder();
$finder->directories()->in(__DIR__)->depth('== 0');
foreach ($finder as $directory){
        $dirs [] = $directory->getRelativePathname();
}
print_r($dirs);

电流输出:

array(
    [0] => FolderA
    [1] => FolderB
    [2] => FolderC
    [3] => FolderD
)
4

1 回答 1

1

一种天真的方法:

<?php

require_once(__DIR__.'/vendor/autoload.php');

use Symfony\Component\Finder\Finder;

$dirs   = [];
$finder = new Finder();
$finder->directories()->in(__DIR__)->depth('== 0');

$requiredFiles = ['aaa.php', 'bbb.php', 'ccc.php'];

foreach ($finder as $directory) {
    $fullPath = $directory->getPathname();

    // if one file is not in this directory, ignore this directory
    foreach ($requiredFiles as $requiredFile) {
        if (!file_exists($fullPath.'/'.$requiredFile)) {
            continue 2;
        }
    }

    $dirs[] = $directory->getRelativePathname();
}

print_r($dirs);

它会输出这个:

Array
(
    [0] => FolderD
    [1] => FolderB
    [2] => FolderA
)

如果您想对文件夹进行排序,只需sort($dirs);foreach块之后调用。

于 2018-05-10T15:10:11.787 回答