- 论坛徽章:
- 0
|
#include <iostream>;
#include <windows.h>;
#include <cstring>;
using namespace std;
const int MAXFILE = 500;
//SROUCE FILE STRUCT
typedef struct _SRCFILE
{
WIN32_FIND_DATA fd;
bool bIsNew;
}SRCFILE;
int main(int argc, char* argv[])
{
if (argv[1] == 0)
{
return (-1);
}
WIN32_FIND_DATA fd;
SRCFILE srcFile[MAXFILE];
bool isfile = true;
int isSrcFile = 0;
HANDLE hFind = INVALID_HANDLE_VALUE;
char DirSpec[100];
strncpy (DirSpec, argv[1], strlen(argv[1])+1);
strncat (DirSpec, "\\*", 3);
char DestDir[100];
strncpy (DestDir, argv[1], strlen(argv[1])+1);
*DestDir = 'E';
CreateDirectory(DestDir, NULL) ;
//deel with the source directory
hFind = FindFirstFile(DirSpec,&fd);
if (hFind == INVALID_HANDLE_VALUE)
{
cout<<"No files!"<<endl;
return (-1);
}
else
{
while (hFind != INVALID_HANDLE_VALUE && isfile)
{
if (fd.dwFileAttributes == FILE_ATTRIBUTE_ARCHIVE)
{
srcFile[isSrcFile].fd = fd;
srcFile[isSrcFile].bIsNew = FALSE;
isSrcFile++;
cout<<fd.cFileName<<endl;
}
isfile = FindNextFile(hFind, &fd);
}
}
//cout<<srcFile[0].fd.cFileName<<":"<<DestDir<<
endl;
//strncat (DestDir, "\\", 2);
if (CopyFile(srcFile[0].fd.cFileName,DestDir,FALSE))
{
cout<<"copy action is ok!"<<endl;
}
else
{
printf("Could not copy file.\n" ;
return 0;
}
测试的结果总是:Could not copy file.看了msdn中该函数的定义及参数要求,只有当目标文件夹是"只读"属性时才会失败,我的系统是xp,好象xp下文件夹属性都是默认只读的,改过后还会还原到只读.不知道是不是这原因. |
|