求C++ 实现把一个文件夹下的所有文件和子文件夹复制到指定的文件夹下

自己是C#开发的一个新手,突然boss急着要写一个C++实现文件夹整体复制的功能,当目标文件存在的话覆盖,不存在的复制,短时间自己真的搞不定,希望大侠帮忙,

C++应该和C#思路一样,先找出源文件夹下的文件,遍历复制,然后找出源文件夹下的所有文件夹递归实现复制;

会的大侠们提供下C++的源代码吧,十分感谢啦

不需要那么麻烦,直接用Shell中的函数拷贝就可以了,只要一行

 #include <windows.h>
#include <shellapi.h>
#include <tchar.h>
#pragma comment(lib, "shell32.lib")

int main()
{
    SHFILEOPSTRUCT fop;
    ZeroMemory(&fop, sizeof fop);
    fop.wFunc = FO_COPY;
    fop.pFrom = _T("c:\\a\0");
    fop.pTo = _T("c:\\b\0");

    SHFileOperation(&fop);

    return 0;
}

参考:http://zhidao.baidu.com/link?url=dUjej5BQz3QEA5D6CSsBoUZqpGHiZ4W28ownst50MqAv3eSmbE6fhRDra_lZcP8Y1D-azvjl-TIXq10GNupc_K

public static void CopyFolder(string strFromPath,string strToPath)
{
//如果源文件夹不存在,则创建
if (!Directory.Exists(strFromPath))
{
Directory.CreateDirectory(strFromPath);
}
//取得要拷贝的文件夹名
string strFolderName = strFromPath.Substring(strFromPath.LastIndexOf("\") +
1,strFromPath.Length - strFromPath.LastIndexOf("\") - 1);
//如果目标文件夹中没有源文件夹则在目标文件夹中创建源文件夹
if (!Directory.Exists(strToPath + "\" + strFolderName))
{
Directory.CreateDirectory(strToPath + "\" + strFolderName);
}
//创建数组保存源文件夹下的文件名
string[] strFiles = Directory.GetFiles(strFromPath);
//循环拷贝文件
for(int i = 0;i < strFiles.Length;i++)
{
//取得拷贝的文件名,只取文件名,地址截掉。
string strFileName = strFiles[i].Substring(strFiles[i].LastIndexOf("\") + 1,strFiles[i].Length - strFiles[i].LastIndexOf("\") - 1);
//开始拷贝文件,true表示覆盖同名文件
File.Copy(strFiles[i],strToPath + "\" + strFolderName + "\" + strFileName,true);
}
//创建DirectoryInfo实例
DirectoryInfo dirInfo = new DirectoryInfo(strFromPath);
//取得源文件夹下的所有子文件夹名称
DirectoryInfo[] ZiPath = dirInfo.GetDirectories();
for (int j = 0;j < ZiPath.Length;j++)
{
//获取所有子文件夹名
string strZiPath = strFromPath + "\" + ZiPath[j].ToString();
//把得到的子文件夹当成新的源文件夹,从头开始新一轮的拷贝
CopyFolder(strZiPath,strToPath + "\" + strFolderName);
}
}

这是C#实现代码,希望可以用C++实现

FinfFirstFile,FinfNextFile遍历目录,然后一个个文件CopyFile()来复制。

你怎么会有这个需求,不过你说的功能很简单 啊 既然思路你都知道了,还做不出来吗?