C语言复制文件夹Windows版
时间: 2023-08-19 09:08:52 浏览: 77
在 C 语言中,可以使用 Windows 系统提供的一些 API 函数来复制文件夹。下面是一个示例代码:
```c
#include <stdio.h>
#include <windows.h>
void CopyFolder(const char* srcPath, const char* destPath) {
char src[MAX_PATH];
char dest[MAX_PATH];
WIN32_FIND_DATAA findData;
HANDLE hFind;
sprintf(src, "%s\\*", srcPath);
sprintf(dest, "%s\\", destPath);
hFind = FindFirstFileA(src, &findData);
if (hFind == INVALID_HANDLE_VALUE) {
printf("无法打开源文件夹\n");
return;
}
do {
if (strcmp(findData.cFileName, ".") != 0 && strcmp(findData.cFileName, "..") != 0) {
char srcFile[MAX_PATH];
char destFile[MAX_PATH];
sprintf(srcFile, "%s\\%s", srcPath, findData.cFileName);
sprintf(destFile, "%s%s", dest, findData.cFileName);
if (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
CreateDirectoryA(destFile, NULL);
CopyFolder(srcFile, destFile);
} else {
CopyFileA(srcFile, destFile, FALSE);
}
}
} while (FindNextFileA(hFind, &findData));
FindClose(hFind);
}
int main() {
const char* srcPath = "源文件夹路径";
const char* destPath = "目标文件夹路径";
CopyFolder(srcPath, destPath);
printf("文件夹复制完成\n");
return 0;
}
```
你需要将代码中的 "源文件夹路径" 替换为要复制的文件夹的路径,将 "目标文件夹路径" 替换为要将文件夹复制到的目标路径。请确保目标路径存在并且有足够的权限进行复制操作。运行代码后,文件夹将被复制到目标路径,并打印出 "文件夹复制完成"。