Core\IO\Entry::isFile
$entry->isFile(): boolChecks whether an entry that Core\IO::walk found is a regular file.
$entry->isFile() returns true for a regular file. It returns false for a folder and for a symbolic link. It also returns false for rare kinds of entry, such as a device or a named pipe.
walk gets the kind of each entry when it reads the folder, so isFile does not read the disk again. The result does not change later. If the file is replaced by a folder with the same name, the entry still returns true. Call Core\IO::isFile($entry->path()) to check the path again.
The examples below label each entry as a file or a folder, count the files in a tree, and write an index of every file.
Return value
Returns bool. true for a regular file. A folder, a symbolic link, a device, a socket and a pipe give false.
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
Files and folders
<?nvs// Shows how to tell the files from the folders that `Core\IO::walk` finds.
string $photos = Core\Path::join(Core\IO::createTemporaryDirectory(), 'photos');Core\IO::makeDirectory(Core\Path::join($photos, '2024'));Core\IO::write(Core\Path::join($photos, '2024', 'beach.jpg'), 'jpg');Core\IO::write(Core\Path::join($photos, 'notes.txt'), 'notes');
// "2024" is a folder, so `isFile()` returns `false` for it.// This prints "2024: folder", "beach.jpg: file" and "notes.txt: file".foreach (Core\IO::walk($photos) as Core\IO\Entry $entry) { echo $entry->name(), ': ', $entry->isFile() ? 'file' : 'folder', "\n";}2024: folderbeach.jpg: filenotes.txt: fileCount the files
<?nvs// Shows how to count only the files in a folder tree.
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', 'empty'));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');
int $files = 0;int $entries = 0;foreach (Core\IO::walk($photos) as Core\IO\Entry $entry) { $entries = $entries + 1; if ($entry->isFile()) { $files = $files + 1; }}
// The tree has six entries. Three of them are folders, so this prints "3 files in 6 entries".echo $files, ' files in ', $entries, ' entries', "\n";3 files in 6 entriesWrite an index
<?nvs// Shows how a program writes an index file with one line for each photo in a folder tree.
string $root = Core\IO::createTemporaryDirectory();string $photos = Core\Path::join($root, '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'), '0123456789');Core\IO::write(Core\Path::join($photos, '2025', 'forest.jpg'), '01234');
// Each line has the relative path and the size in bytes. The folders are left out.string $index = '';foreach (Core\IO::walk($photos) as Core\IO\Entry $entry) { if ($entry->isFile()) { string $relative = Core\Str::replace($entry->relativePath(), Core\Path::SEPARATOR, '/'); $index = $index . $relative . ' ' . Core\IO::size($entry->path()) . "\n"; }}string $indexFile = Core\Path::join($root, 'index.txt');Core\IO::write($indexFile, $index);
// This prints "2024/beach.jpg 10" and "2025/forest.jpg 5".echo Core\IO::readText($indexFile);2024/beach.jpg 102025/forest.jpg 5