Core\IO\Entry::path
$entry->path(): stringReturns the full path of a file or folder that Core\IO::walk found.
$entry->path() is the folder you gave to walk, joined to relativePath(). You can pass it to Core\IO::readText, Core\IO::open, Core\IO::copy or Core\IO::metadata. The start folder is kept exactly as you wrote it. For example, a start folder photos/2024/.. gives paths that start with photos/2024/...
The parts are joined the way Core\Path::join joins them. On Windows, the separator is \, and on other systems it is /. The constant Core\Path::SEPARATOR is the separator of the system the program runs on.
The examples below read a file with path(), show that it is the start folder joined to relativePath(), and copy every photo of one year into an archive folder.
Return value
Returns string. The start folder as you wrote it, joined to the relative path by the rules of Core\Path::join. For a walk of photos, the entry 2024/a.jpg has the path photos/2024/a.jpg. The parts are separated by Core\Path::SEPARATOR, which is \ 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
Read every caption
<?nvs// Shows how to read each text file that `Core\IO::walk` finds, using its full path.
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', 'beach.txt'), "A day at the sea\n");Core\IO::write(Core\Path::join($photos, '2025', 'forest.jpg'), 'jpg');Core\IO::write(Core\Path::join($photos, '2025', 'forest.txt'), "A walk in the forest\n");
// `path()` is the full path of the entry, so `Core\IO::readText` can open it.// This prints the text of the two ".txt" files.foreach (Core\IO::walk($photos) as Core\IO\Entry $entry) { if ($entry->isFile() && Core\Str::endsWith($entry->name(), '.txt')) { echo $entry->name(), ': ', Core\IO::readText($entry->path()); }}beach.txt: A day at the seaforest.txt: A walk in the forestThe start folder and the relative path
<?nvs// Shows that `path()` is the folder you gave to `Core\IO::walk`, joined to `relativePath()`.
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');
// The loop compares `path()` with the start folder joined to `relativePath()`.// They are equal, so this prints "same" for each of the three entries.foreach (Core\IO::walk($photos) as Core\IO\Entry $entry) { string $joined = Core\Path::join($photos, $entry->relativePath()); string $shown = Core\Str::replace($entry->relativePath(), Core\Path::SEPARATOR, '/'); echo $shown, ': ', $entry->path() == $joined ? 'same' : 'different', "\n";}
// The full path starts with the start folder, exactly as you wrote it.// The start folder here ends in `..`. The path of "notes.txt" starts with it, so this prints "true".string $start = Core\Path::join($photos, '2024', '..');foreach (Core\IO::walk($start) as Core\IO\Entry $entry) { if ($entry->name() == 'notes.txt') { echo 'starts with the start folder: ', Core\Str::startsWith($entry->path(), $start) ? 'true' : 'false', "\n"; }}2024: same2024/beach.jpg: samenotes.txt: samestarts with the start folder: trueArchive one year
<?nvs// Shows how a program copies every photo from one year into an archive folder.
string $root = Core\IO::createTemporaryDirectory();string $photos = Core\Path::join($root, 'photos');string $archive = Core\Path::join($root, 'archive');Core\IO::makeDirectory(Core\Path::join($photos, '2024', 'summer'));Core\IO::makeDirectory(Core\Path::join($photos, '2025'));Core\IO::makeDirectory($archive);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, '2025', 'forest.jpg'), 'jpg');
// The walk starts at the "2024" folder, so it finds only the photos of that year.// `path()` is where each photo is now, and `Core\IO::copy` copies it from there.foreach (Core\IO::walk(Core\Path::join($photos, '2024')) as Core\IO\Entry $entry) { if ($entry->isFile()) { Core\IO::copy($entry->path(), Core\Path::join($archive, $entry->name())); }}
// This prints "beach.jpg" and "city.jpg".foreach (Core\Arr::sort(Core\IO::list($archive)) as string $name) { echo $name, "\n";}beach.jpgcity.jpg