Version: v0.7.1 - Beta.  We welcome contributors & feedback.

File.loopDir

File.loopDir($dirPath, $callbackFunction, $options = {}) -> false

Description

Iterate through all files in a directory and apply a function to each.

The $dirPath is relative to the data/files directory.

This function is memory efficient for large directories, because it only handles one item at a time.

Callback Function

The callback function receives a Map of path parts (e.g. dirPath, fileName) of the current file, via File.pathParts.

If the callback function returns true, the loop will stop immediately.

// Count all png files in a directory
$numImages = 0

File.loopDir('some/dir', fn ($file) {
    if $file.fileExt == 'png' {
        $numImages += 1
    }
})

Options

Option Value Description
filter files, dirs, all Type of contents to include
deep true/false Include contents of subdirectories
$fnEachFile = fn ($file) {
    print($file.fullPath)
}

File.loopDir('some/dir', $fnEachFile, { deep, filter: 'dirs' })

See Also