void FolderCopy(char* src,char* dst);//文件夹拷贝,将src文件夹下的所有内容拷贝到dst下,如果不存在dst,则创建之
void CMyCommon::FolderCopy(char* src,char* dst)
{
CreateDirectoryA(dst,NULL);
char srcPath[250];
char dstPath[250];
HANDLE hFind;
WIN32_FIND_DATAA FindFileData;
// char line[1024];
char path[260];
sprintf(path,"%s\\*",src);
hFind = FindFirstFileA(path, &FindFileData);
if(INVALID_HANDLE_VALUE == hFind)
return;
do{
printf("%s\n",FindFileData.cFileName);
if(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
//printf("%s\n",FindFileData.cFileName);
if(FindFileData.cFileName[0] != '.')
{
sprintf(srcPath,"%s\\%s",src,FindFileData.cFileName);
sprintf(dstPath,"%s\\%s",dst,FindFileData.cFileName);
CreateDirectoryA(dstPath,NULL);
this->FolderCopy(srcPath,dstPath);
}
}else {
sprintf(srcPath,"%s\\%s",src,FindFileData.cFileName);
sprintf(dstPath,"%s\\%s",dst,FindFileData.cFileName);
CopyFileA(srcPath,dstPath,TRUE);
}
}while(FindNextFileA(hFind, &FindFileData));
/*
MFC:
SHFILEOPSTRUCT lpFileOp;
lpFileOp.hwnd = GetSafeHwnd();//执行文件复制
lpFileOp.wFunc = FO_COPY;
lpFileOp.pFrom = (LPCWSTR)src;
lpFileOp.pTo = (LPCWSTR)dst;
lpFileOp.fFlags = FOF_ALLOWUNDO;//保存UNDO信息,以便 在回收站中恢复文件
lpFileOp.fAnyOperationsAborted = FALSE;
lpFileOp.hNameMappings = NULL;
lpFileOp.lpszProgressTitle = L"正在复制";
int nReturnValue = SHFileOperation(&lpFileOp);
if(nReturnValue == 0)
{
if(lpFileOp.fAnyOperationsAborted == TRUE){
//"复制文件夹的操作被取消"
printf("复制文件夹的操作被取消\n");
}else {
//"复制文件夹操作成功!"
printf("复制文件夹操作成功!\n");
}
}else {
//复制文件夹操作失败
printf("复制文件夹操作失败\n");
}
*/
}
{
CreateDirectoryA(dst,NULL);
char srcPath[250];
char dstPath[250];
HANDLE hFind;
WIN32_FIND_DATAA FindFileData;
// char line[1024];
char path[260];
sprintf(path,"%s\\*",src);
hFind = FindFirstFileA(path, &FindFileData);
if(INVALID_HANDLE_VALUE == hFind)
return;
do{
printf("%s\n",FindFileData.cFileName);
if(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
//printf("%s\n",FindFileData.cFileName);
if(FindFileData.cFileName[0] != '.')
{
sprintf(srcPath,"%s\\%s",src,FindFileData.cFileName);
sprintf(dstPath,"%s\\%s",dst,FindFileData.cFileName);
CreateDirectoryA(dstPath,NULL);
this->FolderCopy(srcPath,dstPath);
}
}else {
sprintf(srcPath,"%s\\%s",src,FindFileData.cFileName);
sprintf(dstPath,"%s\\%s",dst,FindFileData.cFileName);
CopyFileA(srcPath,dstPath,TRUE);
}
}while(FindNextFileA(hFind, &FindFileData));
/*
MFC:
SHFILEOPSTRUCT lpFileOp;
lpFileOp.hwnd = GetSafeHwnd();//执行文件复制
lpFileOp.wFunc = FO_COPY;
lpFileOp.pFrom = (LPCWSTR)src;
lpFileOp.pTo = (LPCWSTR)dst;
lpFileOp.fFlags = FOF_ALLOWUNDO;//保存UNDO信息,以便 在回收站中恢复文件
lpFileOp.fAnyOperationsAborted = FALSE;
lpFileOp.hNameMappings = NULL;
lpFileOp.lpszProgressTitle = L"正在复制";
int nReturnValue = SHFileOperation(&lpFileOp);
if(nReturnValue == 0)
{
if(lpFileOp.fAnyOperationsAborted == TRUE){
//"复制文件夹的操作被取消"
printf("复制文件夹的操作被取消\n");
}else {
//"复制文件夹操作成功!"
printf("复制文件夹操作成功!\n");
}
}else {
//复制文件夹操作失败
printf("复制文件夹操作失败\n");
}
*/
}
void CreateFolders(char* path,int startNum,int count);//批量创建文件夹
//批量创建文件夹 void CMyCommon::CreateFolders(char* path,int startNum,int count) { char FolderName[256]; for(int i=0; i<count; i++) { sprintf(FolderName,"%s\\%d",path,startNum+i); if(::CreateDirectoryA(FolderName,NULL)){ printf("创建新文件夹 %s 成功!\n",FolderName);//创建成功 }else { printf("创建新文件夹 %s 失败!\n",FolderName); } } }
void RenameFiles(char* path,char* suffix,int startnum);//批量重命名文件
//文件批量重命名 //问题:如果文件夹为1,2,3,此等排列,这方法的执行顺序是1,10,11,。。。 //若为1001开始,则无此问题,会按顺序。 void CMyCommon::RenameFiles(char* path,char* suffix,int startnum) { char suffix1[50] ; if(suffix[0] != '.'){ sprintf(suffix1,".%s",suffix); }else { sprintf(suffix1,"%s",suffix); } char suffixTemp[20] = ".suffix"; char cmd[256]; sprintf(cmd,"del \"%s\\*%s\"",path,suffixTemp); system(cmd); this->RenameFilesFun(path,suffix1,startnum,suffixTemp); this->RenameFilesFun(path,suffixTemp,startnum,suffix1); }
void RenameFilesFun(char* path,char* suffix,int startnum,char* suffixTemp);
void CMyCommon::RenameFilesFun(char* path,char* suffix,int startnum,char* suffixTemp) { int fileNum = startnum; HANDLE hFind; WIN32_FIND_DATAA FindFileData; char oldName[250]; char newName[250]; char lpFileName[260]; sprintf(lpFileName,"%s\\*%s",path,suffix); hFind = FindFirstFileA(lpFileName, &FindFileData); if(INVALID_HANDLE_VALUE == hFind) return; do{ if(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { }else { sprintf(oldName,"%s\\%s",path,FindFileData.cFileName); sprintf(newName,"%s\\%d%s",path,fileNum++,suffixTemp); MoveFileA(oldName,newName); printf("%s------>%s\n",oldName,newName); } }while(FindNextFileA(hFind, &FindFileData)); }
void RenameFolders(char* path,int startnum);//批量重命名文件夹
void CMyCommon::RenameFolders(char* path,int startnum) { this->RenameFoldersFun(path,startnum,"sdlypyzqtemp"); this->RenameFoldersFun(path,startnum,""); }
void RenameFoldersFun(char* path,int startnum,char* temp);
void CMyCommon::RenameFoldersFun(char* path,int startnum,char* temp) { int folderNum = startnum; char oldName[250]; char newName[250]; vector<string> v; this->get_folderlist(path,v); for(int i=0; i<v.size(); i++){ sprintf(oldName,"%s\\%s",path,v[i].data()); sprintf(newName,"%s\\%d%s",path,folderNum++,temp); MoveFileA(oldName,newName); printf("%s------>%s\n",oldName,newName); } /* HANDLE hFind; WIN32_FIND_DATAA FindFileData; char lpFileName[260]; sprintf(lpFileName,"%s\\*",path); hFind = FindFirstFileA(lpFileName, &FindFileData); if(INVALID_HANDLE_VALUE == hFind) return; do{ if(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { if(FindFileData.cFileName[0] != '.') { sprintf(oldName,"%s\\%s",path,FindFileData.cFileName); sprintf(newName,"%s\\%d%s",path,folderNum++,temp); MoveFileA(oldName,newName); printf("%s------>%s\n",oldName,newName); } }else { } }while(FindNextFileA(hFind, &FindFileData)); */ }