Reputation: 9
I need to copy all files from 1 directory to another, but it's not working.
#include <cstdlib>
#include <iostream>
#include <windows.h>
#include <string>
using namespace std;
int main(int argc, char *argv[])
{
TCHAR buffer[256];
SetCurrentDirectory ("C:\\Users\\Rinat\\Desktop\\SP\\1");
GetCurrentDirectory (sizeof (buffer), buffer);
printf ("%s\n", buffer);
WIN32_FIND_DATA FindData;
HANDLE MyFile;
MyFile = FindFirstFile ("*", &FindData);
if (MyFile != INVALID_HANDLE_VALUE) {
do {
printf ("%s\n", FindData.cFileName);
CopyFile("C:\\Users\\Rinat\\Desktop\\SP\\1"+FindData.cFileName, "C:\\Users\\Rinat\\Desktop\\SP\\2\\" + FindData.cFileName, FALSE);
} while (FindNextFile (MyFile, &FindData));
FindClose (MyFile);
}
system("PAUSE");
return EXIT_SUCCESS;
}
The error is 22 C:\Users\Rinat\Desktop\SP\7.cpp invalid operands of types const char[28]
and CHAR[260]
to binary operator+
Upvotes: 0
Views: 57
Reputation: 37192
You can't add string pointers together like that in C++. You need to use a function (or a class like std::string
).
do {
char chSrc[MAX_PATH], cdDst[MAX_PATH];
StringCchCopy(chSrc, MAX_PATH, "C:\\Users\\Rinat\\Desktop\\SP\\1\\");
StringCchCat(chSrc, MAX_PATH, FindData.cFileName);
StringCchCopy(chDst, MAX_PATH, "C:\\Users\\Rinat\\Desktop\\SP\\2\\");
StringCchCat(chDst, MAX_PATH, FindData.cFileName);
CopyFile(chSrc, chDst, TRUE);
} ...
Using std::string
:
do {
CopyFile((std::string("C:\\Users\\Rinat\\Desktop\\SP\\1\\") + FindData.cFileName)).c_str(),
(std::string("C:\\Users\\Rinat\\Desktop\\SP\\2\\") + FindData.cFileName)).c_str(),
TRUE);
} ...
Upvotes: 2