Core\IO::createTemporaryDirectory
Creates a new, empty folder for the files a program only needs while it runs.
Core\IO::createTemporaryDirectory creates a folder and returns its full path. The folder is new and empty, and no other program uses it. Each call creates a different folder. The folders are inside the folder set by [io] temp_root. Without that setting, they are inside the tmp folder in the data folder, .nvsdata next to the nvs program.
When the program ends, Novis deletes each folder it created, with every file in it. You do not need to delete it yourself, but you can. A file that must still exist after the program ends belongs somewhere else.
A program needs no capability for this, on the command line or on a server. If nvs.toml sets [capabilities.fs] write, that list must include the folder for temporary files. Otherwise createTemporaryDirectory throws a RuntimeError. It throws an IOError when the folder cannot be created, or when Novis has no data folder and [io] temp_root is not set.
Return value
Returns string. The full path of the new folder. Each call creates a different folder, and no other program uses it. The folder is under [io] temp_root, or under the tmp folder in the data folder. Novis also deletes it when the script stops with an uncaught error. You may delete it earlier, and that is not an error. A file that must stay after the script ends belongs in another folder.
Errors
RuntimeErrornvs.tomlsets[capabilities.fs] write, and that list does not include the folder for temporary files. The message names the path the list must include.IOErrorThe capability allowed the call, but no directory could be created. The temporary folder is full, read-only or missing. Or there is no temporary folder, because[io] temp_rootis not set and the data folder cannot be used.
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 for scratch files
<?nvs// Shows how `Core\IO::createTemporaryDirectory` creates an empty folder for files the program needs only while it runs.
string $dir = Core\IO::createTemporaryDirectory();
// The folder exists and is empty. This prints "folder: yes, entries: 0".echo 'folder: ', Core\IO::isDirectory($dir) ? 'yes' : 'no', ', entries: ', Core\Arr::count(Core\IO::list($dir)), "\n";
// The program can write and read files in it. This prints "draft saved".string $draft = Core\Path::join($dir, 'draft.txt');Core\IO::write($draft, "draft saved\n");echo Core\IO::readText($draft);folder: yes, entries: 0draft savedEvery call is a new folder
<?nvs// Shows that each call to `Core\IO::createTemporaryDirectory` creates a different folder, and that you may delete one early.
string $first = Core\IO::createTemporaryDirectory();string $second = Core\IO::createTemporaryDirectory();
// The two paths are not the same. This prints "different folders".echo $first == $second ? 'the same folder' : 'different folders', "\n";
// A file in one folder is not in the other. This prints "in the second: no".Core\IO::write(Core\Path::join($first, 'a.txt'), 'a');echo 'in the second: ', Core\IO::exists(Core\Path::join($second, 'a.txt')) ? 'yes' : 'no', "\n";
// Novis deletes both folders when the program ends. You can also delete one yourself.// `removeDirectory` needs an empty folder, so the file is removed first. This prints "first exists: no".Core\IO::remove(Core\Path::join($first, 'a.txt'));Core\IO::removeDirectory($first);echo 'first exists: ', Core\IO::exists($first) ? 'yes' : 'no', "\n";different foldersin the second: nofirst exists: noBuild an export file
<?nvs// Shows how a program writes an export file into a temporary folder before it sends the file.
class Export{ // Writes one line per order to "orders.csv" in a new temporary folder, and returns the file's path. public static function orders(array<string> $orders): string { string $file = Core\Path::join(Core\IO::createTemporaryDirectory(), 'orders.csv'); string $csv = "id\n"; foreach ($orders as string $id) { $csv = $csv . $id . "\n"; } Core\IO::write($file, $csv); return $file; }}
string $file = Export::orders(['A-100', 'A-101', 'A-102']);
// The file has a header line and three orders, so this prints "orders.csv: 21 bytes".// Novis deletes the folder and the file when the program ends.echo Core\Path::fileName($file), ': ', Core\IO::size($file), ' bytes', "\n";orders.csv: 21 bytes