Synchronisation between folders

I want to synchronize two folders/directories. The constrains are follow:

1) Each folder should have at least all files/subfolders from the target source folder.
2) Each folder should contain the newest file/subfolder of both.


I'm at time still unsure, how I could accomplish that. The main problem I have is, that I'm unsure about which container type will be at best suitable for this purpose. Also, I'm unsure about, how I could compare the files of a folder, respective its subfolders.


PLEASE HELP! ;-)

*EDITED
Last edited on
You usually wouldn't compare the contents of the files. You'd generate file hashs and compare the hashs.

For C++17::filesystem, there's functions to obtain the last write time, iterate folders recursively and to copy file(s) between folders. See https://en.cppreference.com/w/cpp/header/filesystem for detials

I'm unsure about which container type will be at best suitable for this purpose.


You don't need a container if you use the C++17::filesystem folder iterate methods.

For which os?
@seeplus wrote:
For which os?


At best it would be platform independent.
The program as such should run on an *X system. The file system at source EXT4 (but I'm not sure), and at target some FAT, i guess. I'ts at a smartphone respective a portable music player (both via usb connection).
Can you get this to list the files on the devices:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <filesystem>
#include <cstdlib>

namespace fs = std::filesystem;

void list_files(const fs::path& dir, int depth=0)
{
    for (auto& p: fs::directory_iterator(dir))
    {
        std::cout << std::setw(4*depth) << "" << p.path() << '\n';
        if (fs::is_directory(p.status()))
            list_files(p.path(), depth + 1);
    }
}

int main(int argc, char** argv)
{
    if (argc != 2)
    {
        std::cerr << "Usage: " << argv[0] << " DIRECTORY\n";
        std::exit(EXIT_FAILURE);
    }

    list_files(argv[1]);
}

Topic archived. No new replies allowed.