Skip to content

Core\IO::copy

Makes a second file with the same content as the first. The first file does not change.

Core\IO::copy takes the file to read and the path of the new file. If a file already exists at the new path, copy replaces it. The copy is a separate file: a later change to one of the two files does not change the other.

A program you start from the command line may copy any file to any path. On a server, a program may read the files in its application folder. It may write only in the folders that Core\IO::createTemporaryDirectory made for it. Other paths need the fs.read capability for the file and the fs.write capability for the new path. Without them, copy throws a RuntimeError and copies nothing. When the system cannot copy, for example because the first file does not exist, copy throws an IOError. A copy of a file onto itself also throws an IOError, and the file stays as it was.

Parameters

  • $fromstringThe file to read. It is left exactly as it was.
  • $tostringThe file to create. Anything already at this path is replaced, as write replaces it.

Return value

Returns void. Nothing. A failure throws an error. It never returns false, so a caller that ignores the result does not miss a failure.

Errors

  • RuntimeErrorThe configuration does not grant fs.read for the source or fs.write for the destination. Both are checked before either is used, so a failed check copies nothing.
  • IOErrorThe capability allowed the call, but the operating system did not. Nothing is at the source, the destination directory does not exist, or the process does not have permission. A destination that is the source file itself, however the path is written, also throws an error. The file stays as it was. The message names both paths.

Examples

Keep a copy before a change

<?nvs
// Shows how to keep a copy of a settings file before a program changes it.
string $dir = Core\IO::createTemporaryDirectory();
string $settings = Core\Path::join($dir, 'settings.ini');
string $backup = Core\Path::join($dir, 'settings.ini.bak');
Core\IO::write($settings, "mode = test\n");
Core\IO::copy($settings, $backup);
Core\IO::write($settings, "mode = live\n");
// The change is only in the first file. This prints "mode = live" and then "mode = test".
echo Core\IO::read($settings);
echo Core\IO::read($backup);
Output
mode = live
mode = test

Copy replaces a file that exists

<?nvs
// Shows that `Core\IO::copy` replaces a file that already exists, and throws an error when the source is missing.
string $dir = Core\IO::createTemporaryDirectory();
string $template = Core\Path::join($dir, 'template.txt');
string $page = Core\Path::join($dir, 'page.txt');
Core\IO::write($template, 'Welcome!');
Core\IO::write($page, 'old text that is much longer');
// This prints "Welcome!". Nothing of the old text is left.
Core\IO::copy($template, $page);
echo Core\IO::read($page), "\n";
// There is no file at this path, so `copy` throws an `IOError`. This prints "not copied".
try {
Core\IO::copy(Core\Path::join($dir, 'missing.txt'), $page);
} catch (IOError $error) {
echo 'not copied', "\n";
}
Output
Welcome!
not copied

A dated backup of each report

<?nvs
// Shows a backup job that copies each report into a backup folder, with the date in the new name.
string $dir = Core\IO::createTemporaryDirectory();
string $reports = Core\Path::join($dir, 'reports');
string $backups = Core\Path::join($dir, 'backups');
Core\IO::makeDirectory($reports);
Core\IO::makeDirectory($backups);
Core\IO::write(Core\Path::join($reports, 'sales.csv'), "month,total\nMay,1200\n");
Core\IO::write(Core\Path::join($reports, 'stock.csv'), "item,count\nchair,40\n");
string $date = '2024-05-31';
foreach (Core\IO::list($reports) as string $name) {
string $to = Core\Path::join($backups, Core\Str::format('%s-%s', $date, $name));
Core\IO::copy(Core\Path::join($reports, $name), $to);
}
// This prints the two new names, "2024-05-31-sales.csv" and "2024-05-31-stock.csv".
foreach (Core\IO::list($backups) as string $name) {
echo $name, "\n";
}
Output
2024-05-31-sales.csv
2024-05-31-stock.csv