Core\IO\Entry::name
$entry->name(): stringReturns the name of a file or folder that Core\IO::walk found, without the folders above it.
The name is the last part of relativePath(). For 2024/beach.jpg, the name is beach.jpg. For a folder, it is the folder's own name, such as 2024. The name never contains a separator, which is the / or \ between folder names.
Use the name to check a file's extension, or to find a file with a known name anywhere in a tree. Use path() to open the file, because the name alone does not say which folder it is in.
The examples below print the names in a photo folder, compare a name with its relative path, and stop the walk at the first file with a given name.
Return value
Returns string. The last part of relativePath(). For 2024/a.jpg, the result is a.jpg.
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
Names in a photo folder
<?nvs// Shows the name of each file and folder 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, '2024', 'city.jpg'), 'jpg');Core\IO::write(Core\Path::join($photos, 'notes.txt'), 'notes');
// A folder's name is printed too. This prints "2024", "beach.jpg", "city.jpg" and "notes.txt".foreach (Core\IO::walk($photos) as Core\IO\Entry $entry) { echo $entry->name(), "\n";}2024beach.jpgcity.jpgnotes.txtThe last part of the path
<?nvs// Shows that `name()` is the last part of `relativePath()`.
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', 'summer', 'beach.jpg'), 'jpg');
// Each line shows the relative path, then the name.// The last line is "2024/summer/beach.jpg -> beach.jpg".foreach (Core\IO::walk($photos) as Core\IO\Entry $entry) { string $relative = Core\Str::replace($entry->relativePath(), Core\Path::SEPARATOR, '/'); echo $relative, ' -> ', $entry->name(), "\n";}2024 -> 20242024/summer -> summer2024/summer/beach.jpg -> beach.jpgFind a photo by name
<?nvs// Shows how a program finds a file with a known name somewhere in a folder tree.
class Photos{ // Returns the relative path of the first file named `$name`, or `null` if there is none. public static function find(string $root, string $name): ?string { foreach (Core\IO::walk($root) as Core\IO\Entry $entry) { if ($entry->isFile() && $entry->name() == $name) { // `return` stops the walk. The walk does not read the folders after the folder of this file. return Core\Str::replace($entry->relativePath(), Core\Path::SEPARATOR, '/'); } } return null; }}
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, '2025', 'forest.jpg'), 'jpg');
// This prints "found: 2025/forest.jpg".echo 'found: ', Photos::find($photos, 'forest.jpg') ?? 'nothing', "\n";
// No file has this name, so the result is `null`. This prints "found: nothing".echo 'found: ', Photos::find($photos, 'mountain.jpg') ?? 'nothing', "\n";found: 2025/forest.jpgfound: nothing