How to use C ++ to copy a directory

I made a backup program using C ++, but it uses the System () command to batch copy files.

I am looking for a way to copy the entire directory (you do not need to create any directories for this, just copy them). Or, conversely, copy everything to a directory.

For example, I want to copy C:\Users\ to E:\Backup\ Or C:\Users\* to E:\Backup\ .

If possible, you can include an example in your answer.

Thanks a lot!

+4
source share
3 answers

The system approach will not be platform independent. I highly recommend using boost :: filesystem for such tasks.

+4
source

1) include .h files:

 #include <csystem> 

2) write cmd:

 system("copy c:\users\ e:\Backup\"); 

Tips: you can write everything in "", just like you copy directories to cmd.

+4
source

To date, the correct solution is C ++ 17 <filesystem> .

 #include <filesystem> int main() { std::filesystem::copy("C:/Users/", "E:/Backup/"); } 

Slashes ( / ) are interpreted as directory separators; this is easier to read than double backslash ( \\ ).

0
source

All Articles