Core\IO\Metadata::isDirectory
$metadata->isDirectory(): boolChecks if a path was a folder.
$info->isDirectory() works on the Core\IO\Metadata value that Core\IO::metadata returns. It returns true when the path was a folder, and false when it was a file or anything else.
isFile checks for a regular file. A folder returns true for isDirectory and false for isFile. Some rare kinds of path, such as a device or a named pipe, return false for both.
The answer is from the moment metadata was called. If the folder is later replaced by a file with the same name, this value still returns true. Call metadata again to check the path again.
Return value
Returns bool. true for a directory, false for anything else that was there.
Changelog
| Version | Change |
|---|---|
0.0.6 | Core\IO writes the word "directory" in full: createTemporaryDirectory, makeDirectory, removeDirectory and isDirectory. Core\IO::metadata returns the Core\IO\Metadata of a file. The old names were temporaryDir, makeDir, removeDir, isDir and stat. |
Examples
A folder or a file
<?nvs// Shows how to check if a path is a folder, using the result of `Core\IO::metadata`.
string $dir = Core\Path::join(Core\IO::createTemporaryDirectory(), 'photos');Core\IO::makeDirectory($dir);string $photo = Core\Path::join($dir, 'beach.jpg');Core\IO::write($photo, 'not a real photo');
// "photos" is a folder. This prints "photos: true".echo 'photos: ', Core\IO::metadata($dir)->isDirectory() ? 'true' : 'false', "\n";
// "beach.jpg" is a file, so it is not a folder. This prints "beach.jpg: false".echo 'beach.jpg: ', Core\IO::metadata($photo)->isDirectory() ? 'true' : 'false', "\n";photos: truebeach.jpg: falseA file where a folder should be
<?nvs// Shows how a program checks that a path is a folder before it writes files into it.
class Output{ // The folder may be missing. Then the method makes it. // If a file has the name, the method throws a `LogicError`. public static function prepare(string $dir): void { if (!Core\IO::exists($dir)) { Core\IO::makeDirectory($dir); } elseif (!Core\IO::metadata($dir)->isDirectory()) { throw new LogicError($dir . ' is a file, not a folder'); } }}
string $reports = Core\Path::join(Core\IO::createTemporaryDirectory(), 'reports');string $exports = Core\Path::join(Core\IO::createTemporaryDirectory(), 'exports');Core\IO::write($exports, "a file with the name of the folder\n");
// "reports" is missing, so it is made. This prints "reports is ready".Output::prepare($reports);echo 'reports is ready', "\n";
// "exports" is a file. This prints "exports is a file, so nothing was written".try { Output::prepare($exports); echo 'exports is ready', "\n";} catch (LogicError $e) { echo 'exports is a file, so nothing was written', "\n";}Core\IO::remove($exports);reports is readyexports is a file, so nothing was writtenPrint a folder tree
<?nvs// Shows how a program prints every file and folder under a folder, with the folders indented.
class Tree{ // The method prints each name. When a name is a folder, the method calls itself for it. public static function show(string $dir, string $indent): void { foreach (Core\IO::list($dir) as string $name) { string $path = Core\Path::join($dir, $name); if (Core\IO::metadata($path)->isDirectory()) { echo $indent, $name, '/', "\n"; Tree::show($path, $indent . ' '); } else { echo $indent, $name, "\n"; } } }}
string $root = Core\Path::join(Core\IO::createTemporaryDirectory(), 'shop');Core\IO::makeDirectory(Core\Path::join($root, 'src/Order'));Core\IO::write(Core\Path::join($root, 'nvs.toml'), '');Core\IO::write(Core\Path::join($root, 'src/main.nvs'), '');Core\IO::write(Core\Path::join($root, 'src/Order/Invoice.nvs'), '');
// This prints 5 lines, one for each name. A folder name ends with "/".Tree::show($root, '');nvs.tomlsrc/ Order/ Invoice.nvs main.nvs