jeffbRTC
jeffbRTC

Reputation: 2069

filesystem error: cannot copy: File exists

I have a .txt file in folder A and I'm trying to copy it to folder B using this code,

#include <iostream>
#include <filesystem>

int main() {
    try {
        std::filesystem::path src("C:\\Projects\\test.txt")
        std::filesystem::path dst("C:\\Projects\\Notes");

        std::filesystem::copy(src, dst, std::filesystem::copy_options::overwrite_existing);
    } catch (const std::exception &e)
    {
        std::cout << e.what() << "\n";
    }
}
                

The code compiles but it outputs an error when I run it.

filesystem error: cannot copy: File exists 
[C:\Projects\test.txt] [C:\Projects\Notes]

Is my usage of filesystem incorrect? Is this a bug of MinGW?

MinGW-w64 9.0.0
GCC Version 11.1

MinGW taken from http://winlibs.com/

My OS is Windows 10

Upvotes: 4

Views: 1861

Answers (1)

jeffbRTC
jeffbRTC

Reputation: 2069

Turns out I have to give the file name and it doesn't automatically copy the file into the folder like with high level languages.

So,

    std::filesystem::path dst("C:\\Projects\\Notes");

becomes

    std::filesystem::path dst("C:\\Projects\\Notes\test.txt");

Upvotes: 1

Related Questions