Skip to content

Core\IO\Entry::relativePath

$entry->relativePath(): string

Returns the path of a file or folder relative to the folder that Core\IO::walk started at.

For the file photos/2024/beach.jpg, a walk of photos gives 2024/beach.jpg. For an entry directly in the start folder, relativePath() returns only its name. The path never starts with a separator, so you can join it to another folder with Core\Path::join.

The folder names are separated by Core\Path::SEPARATOR. That is \ on Windows and / on other systems. To show the same text on every system, replace it with / using Core\Str::replace.

The examples below list a folder tree, walk a sub-folder, and copy a tree into a backup folder with the same folders.

Return value

Returns string. A path such as 2024/a.jpg. It has the folders between the start folder and the entry, then the entry's name. They are separated by Core\Path::SEPARATOR. For an entry directly in the start folder, the result is only its name.

Changelog

VersionChange
0.0.6Core\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 a photo folder

<?nvs
// Shows the path of each entry relative to the folder that `Core\IO::walk` started at.
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');
Core\IO::write(Core\Path::join($photos, 'notes.txt'), 'notes');
// Windows writes `\` between folder names. `Core\Str::replace` changes it to `/`,
// so the output is the same on every system. This prints six paths.
foreach (Core\IO::walk($photos) as Core\IO\Entry $entry) {
echo Core\Str::replace($entry->relativePath(), Core\Path::SEPARATOR, '/'), "\n";
}
Output
2024
2024/beach.jpg
2024/city.jpg
2025
2025/forest.jpg
notes.txt

Walk a sub folder

<?nvs
// Shows that the relative path starts at the folder you give to `Core\IO::walk`.
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');
// A walk of "photos" finds the beach photo at "2024/summer/beach.jpg".
foreach (Core\IO::walk($photos) as Core\IO\Entry $entry) {
if ($entry->name() == 'beach.jpg') {
echo 'from photos: ', Core\Str::replace($entry->relativePath(), Core\Path::SEPARATOR, '/'), "\n";
}
}
// A walk of "photos/2024" finds the same photo at "summer/beach.jpg".
// "city.jpg" is directly in the start folder, so its relative path is its name.
foreach (Core\IO::walk(Core\Path::join($photos, '2024')) as Core\IO\Entry $entry) {
echo 'from 2024: ', Core\Str::replace($entry->relativePath(), Core\Path::SEPARATOR, '/'), "\n";
}
Output
from photos: 2024/summer/beach.jpg
from 2024: city.jpg
from 2024: summer
from 2024: summer/beach.jpg

Back up a folder tree

<?nvs
// Shows how a program copies a folder tree into a backup folder and keeps its sub-folders.
string $root = Core\IO::createTemporaryDirectory();
string $photos = Core\Path::join($root, 'photos');
string $backup = Core\Path::join($root, 'backup');
Core\IO::makeDirectory(Core\Path::join($photos, '2024'));
Core\IO::makeDirectory(Core\Path::join($photos, '2025'));
Core\IO::makeDirectory($backup);
Core\IO::write(Core\Path::join($photos, '2024', 'beach.jpg'), 'jpg');
Core\IO::write(Core\Path::join($photos, '2025', 'forest.jpg'), 'jpg');
// `walk` always gives a folder before the entries inside it. So the loop creates each folder before it copies the files.
// The relative path is the same in both trees.
foreach (Core\IO::walk($photos) as Core\IO\Entry $entry) {
string $target = Core\Path::join($backup, $entry->relativePath());
if ($entry->isDirectory()) {
Core\IO::makeDirectory($target);
} else {
Core\IO::copy($entry->path(), $target);
}
}
// This prints the four entries of the backup folder.
foreach (Core\IO::walk($backup) as Core\IO\Entry $entry) {
echo 'backup/', Core\Str::replace($entry->relativePath(), Core\Path::SEPARATOR, '/'), "\n";
}
Output
backup/2024
backup/2024/beach.jpg
backup/2025
backup/2025/forest.jpg