Core\Path::thisDirectory
Core\Path::thisDirectory(?string $join = null): stringCore\Path::thisDirectory( ?string $join = null, ): string
Returns the full path of the folder that contains the source file with the call.
The compiler writes the path in place of the call, so the call does no work when the program runs. The path is the same from every folder you start the program in. Give thisDirectory a relative path in quotes, such as 'data/rates.json', and it adds that path to the folder. . and .. are removed from the result.
Good to know: the path you add must be a string in quotes, written directly in the code. A variable does not compile. For a part that your program builds, use Core\Path::join(Core\Path::thisDirectory(), $part).
The examples below find a file beside the program, go to the folder above, and join a name that the program builds.
Parameters
$join?stringdefault nullA relative path, written directly in the code as a string such as'data'. It is added to the folder. A variable or a full path does not compile. UseCore\Path::join(Core\Path::thisDirectory(), $part)for a part the program builds.
Return value
Returns string. The full path of the folder, with $join added when you give one, and with its . and .. parts removed.
Errors
RuntimeErrorThe source was given to the compiler as text, so there is no folder to name.
Changelog
| Version | Change |
|---|---|
0.0.6 | Core\Path::parent returns the folder of a path, and Core\Path::fileName returns its last part. fromWorkingDirectory and thisDirectory write their words in full. The old names were dirname, basename, fromCwd and thisDir. |
Examples
A file beside the program
<?nvs// Shows how a program gets the full path of a file in its own folder.
// `thisDirectory` adds `about.md` to the folder of this file.string $about = Core\Path::thisDirectory('about.md');
// The path starts at a root, so this prints `true`.echo Core\Path::isAbsolute($about) ? 'true' : 'false', "\n";
// `about.md` is in the folder of this file, and that folder is named `thisDirectory`.// So this prints `thisDirectory`.echo Core\Path::fileName(Core\Path::parent($about)), "\n";truethisDirectoryThe folder above
<?nvs// Shows how `..` in the path you add goes to the folder above.
// This file is in the folder `Path/thisDirectory`. `..` removes the last part,// so this prints `Path`.echo Core\Path::fileName(Core\Path::thisDirectory('..')), "\n";
// `.` and `..` are removed from the result. This prints `config`.echo Core\Path::fileName(Core\Path::thisDirectory('../shared/../config')), "\n";PathconfigA name the program builds
<?nvs// Shows how to add a file name that the program builds to the folder of this file.
array<string> $months = ['2026-01', '2026-02'];
// `thisDirectory` only takes a path in quotes. For a name the program builds, use `Core\Path::join`.// This prints `2026-01.csv` and `2026-02.csv`.foreach ($months as string $month) { string $path = Core\Path::join(Core\Path::thisDirectory(), 'exports', $month . '.csv'); echo Core\Path::fileName($path), "\n";}2026-01.csv2026-02.csv