Download all files from a directory

1

I have an application that transfers files via socket, but one by one. I need this application to download an entire directory.

I should provide a directory, EX: "c: \ users \ Server \ PASOW_DOWNLOAD"

and then the program should transfer via socket all the files contained in this directory, can anyone give me some information on how I should proceed?

    
asked by anonymous 01.05.2015 / 19:21

2 answers

1

I have a project with something similar to what you want, take a look

  int HYPNOS_Remote_Manip::HYPNOS_FOLDER_DOWNLOAD(std::string dir_remote, Socket_Setup &socket_setup)
{
    HANDLE dir;
    WIN32_FIND_DATA file_data;

    if ((dir = FindFirstFile((dir_remote + "\*").c_str(), &file_data)) == INVALID_HANDLE_VALUE)
        return 1; /* No files found */

    do 
    {
        std::string file_name = file_data.cFileName;
        std::string full_file_name = dir_remote + "\" + file_name;
        const bool is_directory = (file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0;

        if (file_name[0] == '.')
            continue;

        if (is_directory)
            continue;

        if (file_name == "desktop.ini")
            continue;

        int iResult = send(socket_setup.ConnectSocket, file_name.c_str(), strlen(file_name.c_str()), 0);  // Meio obvio, aqui você envia o nome do arquivo para o servidor 
        if (iResult == SOCKET_ERROR)
            break;
        else
            HYPNOS_FILE_DOWNLOAD(full_file_name, socket_setup); // aqui é a função onde você passa o diretório do arquivo em que será enviado para o servidor
    } while (FindNextFile(dir, &file_data));

    FindClose(dir);
    return 0;
}
    
07.05.2015 / 20:51
1

Depending on the format of your directory address, I believe you are on a Windows system. The libraries for C ++ for directory lookup are: FindFirstFile and FindNextFile

Here's a simple example of a directory search:

HANDLE hArquivo;
WIN32_FIND_DATA arquivo;

hArquivo = FindFirstFile("c:\users\Servidor\PASTA_DOWNLOAD\*", &arquivo);

if ( hFile )
{
    do
    {
        String strNomeDoArquivo = arquivo.cFileName

        //Lance teu código de cópia aqui

    }while (FindNextFile(hArquivo, &arquivo) != 0)

    CloseHandle(hArquivo)
}

Note that this code would not be portable for other OS types.

Source: Microsoft

    
02.05.2015 / 10:54