Search code examples
c++vectorfilenameswstringfile-copying

How to get the filename


I want to get the filename from a vector <wstring> after the search operation i want to copy all the files on the vecAviFiles to another path (for example D:\Test).

 int main()
    {
      int                      iRC         = 0;
      std::vector<std::wstring> vecAviFiles;
      std::wstring fileCopiedDestination = L"D:\\";





  // Search 'c:' for '.avi' files including subdirectories
  iRC = SearchDirectory(vecAviFiles, L"c:\\", L"doc");
  if(iRC)
  {
    std::cout << "Error " << iRC << std::endl;
    return -1;
  }

  // Print results
  for(std::vector<std::wstring>::iterator iterAvi = vecAviFiles.begin(); iterAvi != vecAviFiles.end(); ++iterAvi)
    {

        std::wcout << *iterAvi << std::endl;
        copy_file(*iterAvi,fileCopiedDestination + path::filename(*iterAvi));

    }

    return 0;
}

The problem is in this function (it's not working) of the boost library copy_file(*iterAvi,fileCopiedDestination + path::filename(*iterAvi)); how can i get the filename to add it to the fileCopiedDestination path. or how can i do the samething using this function from the WIN API

CopyFileW(Input.c_str(),filename(Input.c_str()) , TRUE);

I think that everything is clear.


Solution

  • This should do what you are looking for:

    path destination = fileCopiedDestination / path(*iterAvi).filename();
    copy_file(*iterAvi, destination);
    

    It would probably be better if you stored vecAviFiles as a vector<path>, then the code would be a little shorter:

    path destination = fileCopiedDestination / iterAvi->filename();