Transfer all files from a directory

Asked

Viewed 117 times

1

I have an application that transferred files via socket, but one at a time. I need this application to transfer an entire directory.

I must provide a directory, EX: "c: users PASTA_DOWNLOAD Server"

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

  • Assuming your code is structured, an idea to solve the problem is: 1) Read the file names in that directory; 2) execute the code you already have in a loop to process each of the files read in step 1)

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;
}

0

According to the address format of your directory, I believe you are on a Windows system. The libraries for C++ for directory search are: Findfirstfile and Findnextfile

See here 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 to other types of OS.

Source: Microsoft

Browser other questions tagged

You are not signed in. Login or sign up in order to post.