Skip to content

Core\IO::move

Moves a file to a new path, or gives it a new name.

Core\IO::move takes two paths. The file at the first path moves to the second path. After the call, the first path is empty. If a file is already at the second path, move replaces it.

The move happens in one step. Another program that reads the second path sees either the old file or the whole new file, and never half of one. Because of this, a program often writes new content to a temporary file first, and then moves it over the real file.

move does not move a file to a different disk. It throws an IOError instead. Use Core\IO::copy and then Core\IO::remove for that. move also throws an IOError when there is no file at the first path, or when the folder of the second path does not exist.

A program you start from the command line may move any file. On a server, a program may write only in the folders that Core\IO::createTemporaryDirectory made for it. Any other path needs the fs.write capability, for both paths. Without it, move throws a RuntimeError.

Parameters

  • $fromstringThe path that stops existing.
  • $tostringThe new path of the file. Anything already there is replaced.

Return value

Returns void. Nothing. The operating system does the rename, so it is atomic. The destination always has either the whole new file or the file it had before. A move between filesystems fails. It is not done as a copy and a delete, because that is not atomic and needs different capabilities. To do that, call copy and then remove.

Errors

  • RuntimeErrorThe configuration does not grant fs.write for one of the two paths. Both are checked before either is used.
  • IOErrorThe capability allowed the call, but the operating system did not. Nothing is at the source, the destination directory does not exist, or the two paths are on different filesystems. The message names both paths.

Examples

Rename a file

<?nvs
// Shows how to give a file a new name with `Core\IO::move`.
string $dir = Core\IO::createTemporaryDirectory();
string $draft = Core\Path::join($dir, 'draft.txt');
string $final = Core\Path::join($dir, 'final.txt');
Core\IO::write($draft, "Dear customer,\n");
Core\IO::move($draft, $final);
// The old name is gone, so this prints "draft.txt: false".
echo 'draft.txt: ', Core\IO::exists($draft) ? 'true' : 'false', "\n";
// The content is now at the new name. This prints "final.txt: Dear customer,".
echo 'final.txt: ', Core\IO::read($final);
Output
draft.txt: false
final.txt: Dear customer,

A missing file throws

<?nvs
// Shows that `Core\IO::move` throws an `IOError` when it cannot move the file.
string $dir = Core\IO::createTemporaryDirectory();
string $missing = Core\Path::join($dir, 'missing.txt');
string $target = Core\Path::join($dir, 'moved.txt');
// There is no file to move, so this prints "nothing to move".
try {
Core\IO::move($missing, $target);
echo 'moved', "\n";
} catch (IOError $e) {
echo 'nothing to move', "\n";
}
// The folder "no-such-folder" does not exist, so the file cannot go there.
// The file stays where it was, and this prints "the file is still here".
string $note = Core\Path::join($dir, 'note.txt');
Core\IO::write($note, "keep me\n");
try {
Core\IO::move($note, Core\Path::join($dir, 'no-such-folder', 'note.txt'));
} catch (IOError $e) {
if (Core\IO::exists($note)) {
echo 'the file is still here', "\n";
}
}
Output
nothing to move
the file is still here

Replace a file safely

<?nvs
// Shows how to replace a file so that a reader never sees a half-written version.
class Settings
{
// Writes the new content to a temporary file next to the real one, and then moves it
// over the real file. The move replaces the old file in one step.
public static function save(string $path, string $content): void
{
string $temporary = $path . '.tmp';
Core\IO::write($temporary, $content);
Core\IO::move($temporary, $path);
}
}
string $path = Core\Path::join(Core\IO::createTemporaryDirectory(), 'settings.json');
Settings::save($path, "{\"theme\": \"light\"}\n");
Settings::save($path, "{\"theme\": \"dark\"}\n");
// This prints the second version: {"theme": "dark"}
echo Core\IO::read($path);
// The temporary file was moved, so it does not exist any more. This prints "false".
echo Core\IO::exists($path . '.tmp') ? 'true' : 'false', "\n";
Output
{"theme": "dark"}
false