Symfony Finder: find all directories except those that start with an underscore
I want to find all directories (at the root level, not recursive) that do not start with an underscore.
getcwd () = / Users / example / project
Example directory:
/Users/example/project/_pictures
/Users/example/project/_graphics
/Users/example/project/test
I tried with the following piece of code:
$directories = $this->container->get('finder')
->directories()
->in(getcwd())
->notName('_*')
->depth(0);
But this seems to be returning:
/Users/example/project/_pictures/home
/Users/example/project/test
So it returns "test", which is correct, but it also returns a subfolder within one that starts with an underscore, which is incorrect. Any ideas what is wrong here?
+3
source to share
3 answers
I found in Finder::searchInDirectory
:
if (static::IGNORE_DOT_FILES === (static::IGNORE_DOT_FILES & $this->ignore)) {
$this->notPaths[] = '#(^|/)\..+(/|$)#';
}
Change this and it works :)
$finder->notPath('#(^|/)_.+(/|$)#')
Update
This code of mine:
$finder = Finder::create()
->files()
->name('*.twig')
->notPath('#(^|/)_.+(/|$)#') // Ignore path start with underscore (_).
->ignoreVCS(true)
->ignoreDotFiles(true)
->ignoreUnreadableDirs()
->in(__DIR__);
foreach ($finder as $file) {
var_dump($file->getRelativePath());
}
+1
source to share