Skip to content

Core\IO\Walk

Every file and folder under a folder, which Core\IO::walk returns. Use it in a foreach loop. Each step reads what it needs and gives one Core\IO\Entry. A second loop over the same value goes on where the first one stopped.

Core\IO\Walk gives every file and folder under one folder, including the ones in its sub-folders. Core\IO::walk returns it. You read it with a foreach loop. Each item is a Core\IO\Entry.

$entry->relativePath() is the path relative to the folder you started from, such as images/logo.svg. On Windows, the folder names are separated by \. Core\Path::SEPARATOR is the separator of the system the program runs on. $entry->path() is the full path, ready for Core\IO::readText. isFile(), isDirectory() and isLink() tell you what the entry is. A symbolic link is an entry, but walk does not follow it.

The order is the same on every system. A folder comes first, then everything inside it, then the next entry of the same folder. The entries of one folder are sorted by name.

The skip option is a list of patterns, such as { skip: ['node_modules', '.git'] }. They use the syntax of Core\Path::matches, and each is compared with relativePath(). A matching entry is not in the result, and walk does not read a matching folder.

walk reads one folder at a time, while the loop runs. A break stops the reading. The second loop over the same result goes on where the first loop stopped. Call walk again to start from the beginning.

walk throws an IOError when the path does not exist or is a file. It throws a RuntimeError when the program may not read the folder. A folder inside the tree that cannot be read throws an IOError in the loop, when the walk reaches it. An entry the program may not read is not in the result.

<h1>Home</h1>
<svg/>
<svg/>
<?nvs
foreach (Core\IO::walk('site') as Core\IO\Entry $entry) {
// This changes `\` to `/`, so the output is the same on every system.
echo Core\Str::replace($entry->relativePath(), Core\Path::SEPARATOR, '/'), "\n";
}
Output
images
images/icons
images/icons/cart.svg
images/logo.svg
index.html

Returned by

You get a Core\IO\Walk from these methods: