inilim / windows-dir-finder
Requires
- php: ^8.4 | ^8.5 | ^8.6
- inilim/di: ^0.1
- inilim/tools: ^0.1
- symfony/process: ^8.1
Requires (Dev)
- phpunit/phpunit: ^13.4
- symfony/finder: ^8.1
Suggests
None
Provides
None
Conflicts
None
Replaces
None
This package is auto-updated.
Last update: 2026-12-01 16:28:34 UTC
README
Fast file and directory lookup on Windows via the native
dircommand. API inspired by Symfony Finder.
About
inilim/windows-dir-finder is a library for traversing the Windows filesystem. Unlike classical implementations that rely on readdir / RecursiveDirectoryIterator, it runs the system dir /s /a command in a single call and parses its output, streaming SplFileInfo objects.
This gives you:
- Speed — one system call instead of thousands of
statcalls. - Laziness — entries are read from the buffer as you iterate; memory stays flat on large trees.
- Flexible filtering — filters are applied both at the whole-output level (
preg_replace) and per entry. - Familiar API — the interface is close to
Symfony\Component\Finder\Finder.
⚠️ The library works on Windows only and relies on the output of the
dircommand (available in all modern versions of Windows).
Requirements
| Component | Version |
|---|---|
| PHP | ^8.4 | ^8.5 | ^8.6 |
| OS | Windows (dir command in PATH) |
| Extensions | standard (no exotic ones) |
Installation
composer require inilim/windows-dir-finder
Quick Start
use Inilim\WinDirFinder\WinDirFinder; $finder = new WinDirFinder(); $finder ->in('D:/projects') ->files() ->name('*.php'); foreach ($finder as $file) { echo $file->getRealPath() . PHP_EOL; }
By default, WinDirFinder searches both files and directories, ignoring hidden items and VCS directories:
// equivalent to $finder = (new WinDirFinder()) ->ignoreDotFiles(true) ->ignoreVCS(true);
Usage Examples
Files only, or directories only
$finder->files(); // search files only $finder->directories(); // search directories only
Multiple root directories
$finder->in([ 'D:/projects', 'D:/work', 'C:/Users/me/Documents', ]);
Duplicate paths are rejected with InvalidArgumentException.
Filtering by name
$finder ->name('*.php') // name match (glob) ->nameRegex('/^src.*/') // name match (regex) ->notName('*Test.php') // name exclusion ->notNameRegex('/^\./'); // regex exclusion
Filtering by path
$finder ->path('/src/') // path contains "/src/" ->pathRegex('/\/tests?\//') // path matches regex ->notPath('/vendor/') // exclude path ->notPathRegex('/\/cache\//');
Ignoring VCS and hidden files
$finder ->ignoreVCS(true) // .git, .svn, .hg, _darcs, etc. ->ignoreDotFiles(true); // anything starting with a dot
The VCS pattern list is extensible:
use Inilim\WinDirFinder\WinDirFinder; WinDirFinder::addVCSPattern(['.idea', '.vscode', '.fleet']);
Sorting
$finder ->sortByName() // by name, case-insensitive ->sortByName(true) // natural sort ->sortBySize() // by size ->sortByModifiedTime() // by mtime ->sort(static fn($a, $b) => $a->getFilename() <=> $b->getFilename()) ->reverseSorting(); // reverse order
💡 When sorting is enabled,
getIterator()returns anArrayIterator, i.e. all results are materialized in memory.
Counting without a full traversal
$count = count($finder); // int
count() uses the summary lines of dir (N File(s), N Dir(s)),
so it does not require a full tree walk.
Materializing to an array
$files = $finder->toArray(); // SplFileInfo[]
Accessing the raw dir output
foreach ($finder->yieldOutputs() as $output) { /** @var \Inilim\WinDirFinder\Output $output */ echo $output->path_to_dir . PHP_EOL; // $output->value — raw dir response text // $output->mode — current mode }
Result Object: SplFileInfo
Every entry is an extension of the native \SplFileInfo
(Inilim\WinDirFinder\SplFileInfo) with additional fields:
| Property | Type | Description |
|---|---|---|
output_pathname |
string |
Full path to the file/directory |
output_filename |
string |
Name without path |
output_is_dir |
bool |
true — directory, false — file |
output_size |
?int |
Size in bytes for files, null for directories |
output_modified_at |
\DateTimeImmutable |
Last modification time |
search_dir |
string |
Root directory where the entry was found |
Example:
foreach ($finder as $item) { printf( "%-30s %s %s\n", $item->output_filename, $item->output_is_dir ? '[DIR] ' : '[FILE]', $item->output_is_dir ? $item->output_modified_at->format('Y-m-d H:i') : $item->output_size . ' B' ); }
API Reference
WinDirFinder
| Method | Description |
|---|---|
__construct() |
Creates a finder with mode FILES_AND_DIRS | IGNORE_DOT_FILES | IGNORE_VCS_FILES |
static addVCSPattern(string|array $pattern): void |
Adds VCS patterns to the global list |
in(string|array $dirs): self |
Sets search directories (throws InvalidArgumentException if a path does not exist) |
files(): self |
Files only |
directories(): self |
Directories only |
ignoreDotFiles(bool $ignore): self |
Toggles ignoring hidden files |
ignoreVCS(bool $ignore): self |
Toggles ignoring VCS directories |
name(string|array $names): self |
Match by name |
nameRegex(string|array $regexes): self |
Match by name regex |
notName(string|array $names): self |
Exclude by name |
notNameRegex(string|array $regexes): self |
Exclude by name regex |
path(string|array $paths): self |
Match by path |
pathRegex(string|array $regexes): self |
Match by path regex |
notPath(string|array $paths): self |
Exclude by path |
notPathRegex(string|array $regexes): self |
Exclude by path regex |
sort(\Closure(SplFileInfo,SplFileInfo):int $callback): self |
Custom sort |
sortByName(bool $useNaturalSort = false): self |
Sort by name |
sortBySize(): self |
Sort by size |
sortByModifiedTime(): self |
Sort by mtime |
reverseSorting(): self |
Reverse order |
getIterator(): \Traversable |
Entry iterator |
count(): int |
Number of entries |
toArray(): SplFileInfo[] |
Array of entries |
yieldOutputs(): \Generator |
Stream of raw Output (one per root directory) |
All filters combine via AND; within one group (names, nameRegex, etc.) — OR.
Mode Constants
Inilim\WinDirFinder\Mode is a bitmask that defines finder behavior.
| Constant | Bit | Meaning |
|---|---|---|
FILES_AND_DIRS |
1 << 0 |
Search files and directories |
ONLY_FILES |
1 << 1 |
Files only |
ONLY_DIRS |
1 << 2 |
Directories only |
IGNORE_DOT_FILES |
1 << 3 |
Ignore hidden |
REVERSE_SORTING |
1 << 4 |
Reverse sorting |
IGNORE_VCS_FILES |
1 << 5 |
Ignore VCS |
SORT |
1 << 6 |
Sorting enabled |
PrepareMode Constants
Inilim\WinDirFinder\PrepareMode — internal phases of parsing the dir output.
| Constant | Meaning |
|---|---|
FIRST |
Initial normalization |
CREATE_TAGS |
Insertion of <dir>, <e>, <n>, etc. |
LAST_INFORMATION |
Extraction of trailing summary lines |
TOTAL_FILES / TOTAL_DIRS / TOTAL_ENTRIES |
Aggregates cache |
Limitations
- Windows only. On other OSes
yieldOutputs()throwsInvalidArgumentException. - Requires the
dircommand to be available inPATH. - Requires the process to run under an account with read access to the target directories.
- File/directory names with leading spaces are preserved as-is — the library does not trim them.
Testing
composer tests composer stan
License
MIT. See LICENSE.