Core\IO\Entry::isDirectory
$entry->isDirectory(): boolChecks whether an entry that Core\IO::walk found is a folder.
$entry->isDirectory() returns true for a folder. It returns false for a file. It also returns false for a symbolic link to a folder. It also returns false for a junction, which is a Windows link to a folder. walk does not go into a link.
walk gets the kind of each entry when it reads the folder, so isDirectory does not read the disk again. walk gives each folder before the entries inside it. An empty folder is in the result too.
The examples below list the folders of a tree, print a tree with a / after each folder name, and count the photos in each folder.
Return value
Returns bool. true for a folder. A symbolic link to a folder gives false. So does a junction, which is a kind of link to a folder on Windows.
Changelog
| Version | Change |
|---|---|
0.0.6 | Core\IO::walk gives a Core\IO\Entry for each file and folder. path(), relativePath() and name() give the full path, the path from the start folder and the last part. isFile(), isDirectory() and isLink() tell you what the entry is. |
Examples
List the folders
<?nvs// Shows how to find only the folders in a folder tree.
string $photos = Core\Path::join(Core\IO::createTemporaryDirectory(), 'photos');Core\IO::makeDirectory(Core\Path::join($photos, '2024', 'summer'));Core\IO::makeDirectory(Core\Path::join($photos, '2025'));Core\IO::write(Core\Path::join($photos, '2024', 'summer', 'beach.jpg'), 'jpg');Core\IO::write(Core\Path::join($photos, 'notes.txt'), 'notes');
// "2025" is empty, and it is still a folder. The files are left out.// This prints "2024", "2024/summer" and "2025".foreach (Core\IO::walk($photos) as Core\IO\Entry $entry) { if ($entry->isDirectory()) { echo Core\Str::replace($entry->relativePath(), Core\Path::SEPARATOR, '/'), "\n"; }}20242024/summer2025Print a tree
<?nvs// Shows how to print a folder tree with each name indented below its folder.
string $photos = Core\Path::join(Core\IO::createTemporaryDirectory(), 'photos');Core\IO::makeDirectory(Core\Path::join($photos, '2024', 'summer'));Core\IO::write(Core\Path::join($photos, '2024', 'city.jpg'), 'jpg');Core\IO::write(Core\Path::join($photos, '2024', 'summer', 'beach.jpg'), 'jpg');Core\IO::write(Core\Path::join($photos, 'notes.txt'), 'notes');
echo 'photos/', "\n";foreach (Core\IO::walk($photos) as Core\IO\Entry $entry) { // Each folder that contains the entry adds two spaces. uint $depth = Core\Arr::count(Core\Str::split($entry->relativePath(), Core\Path::SEPARATOR)); string $indent = Core\Str::repeat(' ', $depth); // A folder name gets a `/` after it. echo $indent, $entry->name(), $entry->isDirectory() ? '/' : '', "\n";}photos/ 2024/ city.jpg summer/ beach.jpg notes.txtPhotos per folder
<?nvs// Shows how a program counts the photos in each folder of a photo collection.
string $photos = Core\Path::join(Core\IO::createTemporaryDirectory(), 'photos');Core\IO::makeDirectory(Core\Path::join($photos, '2024'));Core\IO::makeDirectory(Core\Path::join($photos, '2025'));Core\IO::write(Core\Path::join($photos, '2024', 'beach.jpg'), 'jpg');Core\IO::write(Core\Path::join($photos, '2024', 'city.jpg'), 'jpg');Core\IO::write(Core\Path::join($photos, '2025', 'forest.jpg'), 'jpg');
// `path()` of a folder is a full path, so `Core\IO::list` can read the names in it.// This prints "photos in 2024: 2" and "photos in 2025: 1".foreach (Core\IO::walk($photos) as Core\IO\Entry $entry) { if ($entry->isDirectory()) { int $count = 0; foreach (Core\IO::list($entry->path()) as string $name) { if (Core\Str::endsWith($name, '.jpg')) { $count = $count + 1; } } echo 'photos in ', $entry->name(), ': ', $count, "\n"; }}photos in 2024: 2photos in 2025: 1