Core\IO::walk
Core\IO::walk(string $path, {skip?: array<string>}): Core\IO\WalkCore\IO::walk( string $path, {skip?: array<string>}, ): Core\IO\Walk
Returns every file and folder inside a folder, including the ones in its sub-folders.
Core\IO::walk gives one Core\IO\Entry for each file and folder. relativePath() is the path from the starting folder, such as images/logo.png, and path() is the full path. A symbolic link is an entry, and its isLink() is true. walk does not read what the link points to.
The order is the same on every system. A folder comes before everything inside it. The names inside one folder are sorted by their bytes, so B.txt comes before a.txt. walk reads one folder at a time while the loop runs. After a break, a second loop over the same value continues with the next entry.
The skip option takes patterns in the syntax of Core\Path::matches. An entry whose relativePath() matches is left out. walk does not read what is inside a matching folder.
walk throws an IOError when the path does not exist or is a file. The result never contains an entry the program may not read. On a server, walk leaves out nvs.toml and the names that start with .. A folder outside the application folder needs the fs.read capability, or walk throws a RuntimeError.
Parameters
$pathstringThe folder the walk starts at. The call reads it at once. A file throws an error, as it does forlist.
Options
skiparray<string>optionalPatterns for the entries to leave out, in the syntax ofCore\Path::matches. Each pattern is compared with the entry'srelativePath().['node_modules', '.git']leaves out both folders, and the walk never opens them.['*.tmp']leaves out every.tmpfile. The default is[].
Return value
Returns Core\IO\Walk. A Core\IO\Walk to use in a foreach loop. Each step gives one Core\IO\Entry. The order is depth-first. Each folder comes first, then everything inside it, and then the next entry of the folder that contains it. The names in one folder come in the order of their bytes, so B comes before a. The walk leaves out every file and folder the program may not read, and it never opens such a folder.
Errors
RuntimeErrorThe configuration does not grantfs.readfor the start folder, or askippattern is not valid. For a pattern, the message gives the position of the problem. The call throws this error before the loop starts.IOErrorThe capability allowed the call, but the operating system did not. Nothing is at the path, or it is not a folder, and the call throws. A folder inside the tree that cannot be read throws this error in the loop, at the step that reaches it.
Changelog
| Version | Change |
|---|---|
0.0.6 | Core\IO::walk reads one folder at a time while the foreach loop runs, and gives a Core\IO\Entry for each file and folder. The order is the same on every system: a folder, then everything inside it, with names sorted. The option { skip: ['node_modules', '.git'] } leaves out matching entries, and walk does not read a matching folder. |
Examples
Walk a folder tree
<?nvs// Shows how `Core\IO::walk` returns every file and folder in a folder and in all its sub-folders.
string $site = Core\Path::join(Core\IO::createTemporaryDirectory(), 'site');Core\IO::makeDirectory(Core\Path::join($site, 'images', 'icons'));Core\IO::write(Core\Path::join($site, 'index.html'), "<h1>Home</h1>\n");Core\IO::write(Core\Path::join($site, 'images', 'logo.png'), 'png');Core\IO::write(Core\Path::join($site, 'images', 'icons', 'cart.svg'), '<svg/>');
// The order is the same on every system: a folder, then everything inside it.// The names inside one folder are sorted. This prints five paths.foreach (Core\IO::walk($site) as Core\IO\Entry $entry) { // `relativePath()` uses `\` on Windows. `Core\Str::replace` changes it to `/`. string $shown = Core\Str::replace($entry->relativePath(), Core\Path::SEPARATOR, '/'); echo $entry->isDirectory() ? 'folder ' : 'file ', $shown, "\n";}folder imagesfolder images/iconsfile images/icons/cart.svgfile images/logo.pngfile index.htmlSkip folders
<?nvs// Shows how the `skip` option leaves folders out of a walk, such as `node_modules` and `.git`.
string $app = Core\Path::join(Core\IO::createTemporaryDirectory(), 'app');Core\IO::makeDirectory(Core\Path::join($app, 'src'));Core\IO::makeDirectory(Core\Path::join($app, 'node_modules', 'lib'));Core\IO::makeDirectory(Core\Path::join($app, '.git', 'objects'));Core\IO::write(Core\Path::join($app, 'src', 'main.js'), 'run();');Core\IO::write(Core\Path::join($app, 'node_modules', 'lib', 'index.js'), 'lib();');Core\IO::write(Core\Path::join($app, '.git', 'HEAD'), 'main');
// Without `skip`, the walk returns all 8 entries. This prints "all: 8".uint $all = 0;foreach (Core\IO::walk($app) as Core\IO\Entry $entry) { $all = $all + 1;}echo 'all: ', $all, "\n";
// A pattern without `/` matches a name in any folder. `walk` does not read inside a matching folder.// This prints "src" and "src/main.js".foreach (Core\IO::walk($app, { skip: ['node_modules', '.git'] }) as Core\IO\Entry $entry) { echo Core\Str::replace($entry->relativePath(), Core\Path::SEPARATOR, '/'), "\n";}all: 8srcsrc/main.jsSize of an uploads folder
<?nvs// Shows how a program adds up the size of every image in an uploads folder and its sub-folders.
class Uploads{ // This function returns the total size in bytes of the ".jpg" files anywhere under `$root`. // Folders and other files are left out. public static function imageBytes(string $root): uint { uint $total = 0; foreach (Core\IO::walk($root) as Core\IO\Entry $entry) { // `path()` is the full path of the entry. `Core\IO::metadata` reads its size. if ($entry->isFile() && Core\Str::endsWith($entry->name(), '.jpg')) { $total = $total + Core\IO::metadata($entry->path())->size(); } } return $total; }}
string $uploads = Core\Path::join(Core\IO::createTemporaryDirectory(), 'uploads');Core\IO::makeDirectory(Core\Path::join($uploads, 'users', 'anna'));Core\IO::makeDirectory(Core\Path::join($uploads, 'users', 'ben'));Core\IO::write(Core\Path::join($uploads, 'banner.jpg'), '0123456789');Core\IO::write(Core\Path::join($uploads, 'users', 'anna', 'avatar.jpg'), '01234');Core\IO::write(Core\Path::join($uploads, 'users', 'ben', 'avatar.jpg'), '0123');Core\IO::write(Core\Path::join($uploads, 'users', 'ben', 'notes.txt'), 'not an image');
// The three images have 10, 5 and 4 bytes, so this prints "images: 19 bytes".echo 'images: ', Uploads::imageBytes($uploads), ' bytes', "\n";images: 19 bytes