各位大虾,现在我有个问题需要问一下。我想复制一个文件夹的文件(源文件夹,比如c:\temp)到另外一个文件夹(目标文件夹,比如d:\temp)里,当存在相同文件名的文件时(比如c:\temp\test\test.cpp,和d:\temp\test\test.cpp),要求仅当源文件的最后修改时间比目标文件的最后修改时间迟时,才覆盖这个文件。请问怎么实现?多谢

解决方案 »

  1.   

    先检查C:\temp中的文件在d:\temp中是否存在。有的话,就分别读取这两个文件的最后修改时间进行比较,然后决定是否覆盖
      

  2.   

    这个问题很简单!用CFileFind,做递归
    CFileFind find;
    CFile file;
    CFileStatus srcfilestatus,dstfilestatus;
    int nOK = find.FindFile(c:\\temp\\*.*);
    while(nOK)
    {
    nOK = find.FindNextFile();
    if(find.IsDots())continue;
    if(find.IsDirectory())
    {
        //调用自身递归
    }
    else
    {
    if(file.Open(str,CFile::modeRead))
    {
    file.GetStatus(srcfilestatus);
    file.Close();
    }
    if(file.Open(str1,CFile::modeRead))
    {
    file.GetStatus(dstfilestatus);
    file.Close();
    }
    if(dstfilestatus.m_mtime >= srcfilestatus.m_mtime)
    ::CopyFile(str,str1,1);
    }
    }                      大概的帮你写了一下,如果有问题给我留言或加我MSN :[email protected]
      

  3.   

    可惜公司不能用MSN,不过能用Qq
      

  4.   

    一样一样! 我们公司只能用MSN,不能用QQ!
      

  5.   

    还是没有,那只有通过邮箱了,[email protected]
      

  6.   

    我实在做不出了,还是有请高手继续帮忙,如果能有demo code 最好了
      

  7.   

    it seems that you can solve it by using the api function "SHFileOperation"
      

  8.   

    WINSHELLAPI int WINAPI SHFileOperation(
        LPSHFILEOPSTRUCT lpFileOp
    ); Copies, moves, renames, or deletes a file system object. Returns zero if successful, or nonzero otherwise. 
    lpFileOp 
    Address of an SHFILEOPSTRUCT structure that contains information this function needs to carry out the specified operation. 
      

  9.   

    GetStatus(CFileStatus& rStatus  )
    得到的结构里面有标记修改时间的字段,叫m_mtime   
      

  10.   

    int gi_mlen;//-----------------------------------------------------------------------------------------------------------
    //说明:对比文件的修改日期
    //参数:1.做比较的目标文件路径 2. 要做比较的源文件路径
    //返回:目标修改日期大于源文件修改日期,返回1,否则返回0
    //-----------------------------------------------------------------------------------------------------------
    int ContrastFile(CString dst,CString src)
    {
    CFile file;
    CFileStatus srcfilestatus,dstfilestatus; if(file.Open(src,CFile::modeRead)) //读取源文件的文件属性
    {
    file.GetStatus(srcfilestatus);
    file.Close();
    }
    if(file.Open(dst,CFile::modeRead)) //读取目标文件的文件属性
    {
    file.GetStatus(dstfilestatus);
    file.Close();
    }
    if(dstfilestatus.m_mtime > srcfilestatus.m_mtime) //目标文件和源文件对比修改日期
    return 1;

    return 0;
    }//-----------------------------------------------------------------------------------------------------------
    //说明:用作递归
    //参数:1. 目标文件及递归路径 2. 要做比较的文件
    //返回:无
    //-----------------------------------------------------------------------------------------------------------
    void Resu(CString dst,CString src) //递归函数
    { CFileFind find;
    CString dir = src;
    int nOK = find.FindFile(dst);
    while(nOK)
    {
    nOK = find.FindNextFile(); //继续查找下一文件
    if(find.IsDots())continue; //如果查到文件为 [.] 或 [..] 则忽略
    if(find.IsDirectory())
    {
    CString tmp = find.GetFilePath(); //如果查到为文件夹,则在文件夹后面加上 \*.* if(src.ReverseFind('\\')==src.GetLength())src += '\\';
    dir += tmp.Mid(gi_mlen); //注意这里,最好以递归名做截取操作(截取一个变量)
    CreateDirectory(dir,NULL);
    tmp.Insert(tmp.GetLength(),"\\*.*");
    Resu(tmp,src); //用来做继续递归调用
    }
    else
    {
    CString tmp = find.GetFilePath(); //如果查找到的 是文件类型
    tmp.MakeLower(); //将路径转换成小写 (主要用作字符串对比) dir = src+tmp.Mid(gi_mlen); if(ContrastFile(tmp,dir)) //对比文件目标日期是否新于原文件日期
    CopyFile(tmp,dir,1); //第三个参数为1 = 覆盖式拷贝

    }
    }
    }