Search by

inilim / windows-dir-finder

inilim

Package info

github.com/inilim/windows-dir-finder

pkg:composer/inilim/windows-dir-finder

Statistics

Installs: 2

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

0.0.20261201 2026-12-01 16:23 UTC

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 dir command. API inspired by Symfony Finder.

PHP Version License Platform

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 stat calls.
  • 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 dir command (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 an ArrayIterator, 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() throws InvalidArgumentException.
  • Requires the dir command to be available in PATH.
  • 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.