Core\IO::list
Returns the names of the files and folders inside a folder.
Core\IO::list reads one folder and returns an array with one string for each file and folder directly inside it. Each string is only the name, such as report.pdf, without the folder path. Use Core\Path::join to build the full path again. . and .. are never in the result, and list does not look inside the folders it finds.
The names are sorted by their bytes, and the order is the same on every system. So B.txt comes before a.txt.
list throws an IOError when the path does not exist or is a file.
The result never contains a name the program may not read. A program you start from the command line lists every name. On a server, a program may read only its application folder, except nvs.toml and names that start with ., such as .env and .git. list leaves these names out. read = ["."] under [capabilities.fs] in nvs.toml lets the program read the whole application folder, with the names that start with .. Then list returns them too. Any other folder needs the fs.read capability. Without it, list throws a RuntimeError.
Parameters
$pathstringThe directory to read. If$pathis a file, the call throws anIOError.
Return value
Returns array<string>. One string per entry. Each one is a name, not a path. To get a path, join the name to $path with Core\Path::join. If the name came from outside the program, use Core\IO::within for that join. It throws an error for a name that leads out of the folder. The whole list is in memory at once. walk reads a tree one folder at a time.
Errors
RuntimeErrorThe configuration does not grantfs.readfor this path.IOErrorThe capability allowed the call, but the operating system did not. Nothing is at the path, it is not a directory, or an entry could not be read partway through the listing.
Changelog
| Version | Change |
|---|---|
0.0.6 | Core\IO::list sorts the names by their bytes, so the order is the same on every system. It leaves out every file and folder the program may not read, and Core\IO::walk does the same in every folder. So server work does not see .env, .git or nvs.toml unless nvs.toml grants them, for example with read = ["."]. |
Examples
List a folder
<?nvs// Shows how `Core\IO::list` returns the name of each file and folder inside a folder.
string $site = Core\Path::join(Core\IO::createTemporaryDirectory(), 'site');Core\IO::makeDirectory(Core\Path::join($site, 'images'));Core\IO::write(Core\Path::join($site, 'index.html'), "<h1>Home</h1>\n");Core\IO::write(Core\Path::join($site, 'style.css'), "h1 { color: navy; }\n");Core\IO::write(Core\Path::join($site, 'README.md'), "# Site\n");
// Each entry is only a name. The names are sorted by their bytes, so uppercase letters come first.// This prints "README.md", "images", "index.html" and "style.css".foreach (Core\IO::list($site) as string $name) { echo $name, "\n";}README.mdimagesindex.htmlstyle.cssEmpty folders and files
<?nvs// Shows what `Core\IO::list` returns for an empty folder, and what happens when the path is a file.
string $dir = Core\Path::join(Core\IO::createTemporaryDirectory(), 'inbox');Core\IO::makeDirectory($dir);
// The folder is empty, so `list` returns an empty array. This prints "entries: 0".echo 'entries: ', Core\Arr::count(Core\IO::list($dir)), "\n";
// "." and ".." are never in the result. After one file is written, this prints "entries: 1".Core\IO::write(Core\Path::join($dir, 'message.txt'), "hello\n");echo 'entries: ', Core\Arr::count(Core\IO::list($dir)), "\n";
// "message.txt" is a file, not a folder. `list` throws an `IOError`,// and this prints "not a folder".try { Core\IO::list(Core\Path::join($dir, 'message.txt'));} catch (IOError $e) { echo 'not a folder', "\n";}entries: 0entries: 1not a folderReport log files
<?nvs// Shows how a program finds the log files in a folder and adds up their size.
class Logs{ // This function returns the names of the files in `$dir` that end with ".log", sorted by name. // Folders and other files are left out. public static function find(string $dir): array<string> { array<string> $found = []; foreach (Core\IO::list($dir) as string $name) { if (Core\Str::endsWith($name, '.log') && Core\IO::isFile(Core\Path::join($dir, $name))) { $found[] = $name; } } return $found; }}
string $logs = Core\Path::join(Core\IO::createTemporaryDirectory(), 'logs');Core\IO::makeDirectory(Core\Path::join($logs, 'archive'));Core\IO::write(Core\Path::join($logs, 'app.log'), "started\nstopped\n");Core\IO::write(Core\Path::join($logs, 'error.log'), "disk full\n");Core\IO::write(Core\Path::join($logs, 'notes.txt'), "not a log\n");
// This prints "app.log: 16 bytes" and "error.log: 10 bytes", and then "total: 26 bytes".uint $total = 0;foreach (Logs::find($logs) as string $name) { uint $size = Core\IO::size(Core\Path::join($logs, $name)); echo $name, ': ', $size, ' bytes', "\n"; $total = $total + $size;}echo 'total: ', $total, ' bytes', "\n";app.log: 16 byteserror.log: 10 bytestotal: 26 bytes