Snippet. Vala. Copying One File to Another

This snippet shows how to copy one file into another. However, be aware that the content of the second file will be lost. Thus if you need the file make a copy first.

var source = File.new_for_path("source.txt");
var destination = File.new_for_path("destination.txt");

if(source.query_exists() == true){
    try {
        source.copy(destination, FileCopyFlags.NONE);
    } catch (Error e) {
        // Error occurred
    }
}

Updated on: 27 Dec 2024