C/C++实现文件的备份
2013-05-11 14:12
393 查看
/*以下代码实现从源目录按照目录层级将文件备份到目的地目录*/ #include "iostream" #include "windows.h" using namespace std; void CopyFolder(char path[], char Destpath[],int level = 0) { char find_path[256]; char Dest_path[256]; char Dest_pathCopy[256]; char srcPath[256]; sprintf(find_path, "%s*", path); sprintf(Dest_path, "%s", Destpath); WIN32_FIND_DATA FindFileData; HANDLE hFind; BOOL bContinue = TRUE; hFind = FindFirstFile(find_path, &FindFileData); if (hFind == INVALID_HANDLE_VALUE) { return; } while (bContinue) { if (stricmp(FindFileData.cFileName, "..") && stricmp(FindFileData.cFileName, ".")) { for (int i = 0; i < level; ++i) cout<<" "; cout<<"1"<<FindFileData.cFileName<<endl; // 如果是目录的话:.创建新目录;.递归查找目录下文件 // 如果是文件的话:复制文件到目的地目录下 if (FindFileData.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY) { sprintf(find_path, "%s%s\\", path, FindFileData.cFileName); sprintf(Dest_path, "%s%s\\", Destpath, FindFileData.cFileName); ::CreateDirectory(Dest_path, NULL); CopyFolder(find_path, Dest_path, level + 1); } else { strcpy(Dest_pathCopy, Destpath); char *pos = strrchr(Dest_pathCopy, '\\'); if ( NULL != pos ) { ++pos; *pos = '\0'; } strcat(Dest_pathCopy, FindFileData.cFileName); strcpy(srcPath, path); pos = strchr(srcPath, '*'); // ++pos; if ( NULL != pos ) { *pos = '\0'; } strcat(srcPath, FindFileData.cFileName); BOOL ret = ::CopyFile(srcPath, Dest_pathCopy, FALSE); } } bContinue = FindNextFile(hFind, &FindFileData); } } int main() { char szPath[] = "E:\\Film\\"; char destPath[] = "F:\\NEW_Film\\"; CopyFolder(szPath, destPath); return 0; }
相关文章推荐
- rsync实现文件备份同步(比如服务器镜像)
- 【头文件】c++实现链表
- 生成日志文件,并在0点进行备份(java 实现)
- 使用cwRsync实现windows下文件定时同步(备份)
- rsync实现文件备份同步
- C++的头文件和实现文件分别写什么
- linux中部署rsync实现文件远程备份及同步
- RSync实现文件备份同步
- C++的头文件和实现文件分别写什么
- Windows中利用计划任务实现自动备份文件
- c++ 实现对配置文件的读写 根据windows API 函数简单改写
- C++的头文件和实现文件分别写什么
- 利用rdiff-backup实现远程备份文件
- RSync实现文件备份同步
- 用批处理命令加WinRAR实现自动备份文件数据
- C++/Php/Python/Shell 程序按行读取文件或者控制台的实现
- 通过Robocopy+DOS 命令+Windows排程实现自动备份(将特定文件/目录备份至自动创建的以年月日命名的目标目录)
- RSync实现文件备份同步
- U盘小偷——C++实现U盘插入检测和文件扫描拷贝
- C++ 为什么模板只能在头文件中实现?