提取出A文件夹内与B文件夹不同的文件

来源:互联网 发布:hadoop sql 编辑:程序博客网 时间:2024/05/17 06:30

提取出A文件夹内与B文件夹不同的文件

相信大家在工作中遇见过这种情形,需要找出两个文件夹内不同的文件。

以下代码供大家参考:

本Markdown编辑器使用[StackEdit][6]修改而来,用它写博客,将会带来全新的体验哦:

#define  _CRT_SECURE_NO_WARNINGS #include <vector>#include "io.h"#include <iostream>#include <fstream>#include <algorithm>#include <windows.h>#include "Shlwapi.h"#include "tchar.h"#pragma comment(lib,"Shlwapi.lib") /*需要加上此行才可以正确link */using namespace std;int flag = 0;//将新版本文件夹下的所有文件名放入vec,包括子目录的文件名void CubeGetFileName(char *lpPath, std::vector<string> &fileList,char *srtRootPathName){    char szFind[MAX_PATH];    WIN32_FIND_DATAA FindFileData;    strcpy(szFind, lpPath);    strcat(szFind, "\\*.*");    HANDLE hFind = ::FindFirstFileA(szFind, &FindFileData);    if (INVALID_HANDLE_VALUE == hFind)            return;    while (true)    {        char szFile[MAX_PATH];        string strReletivePath;        if (FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)        {            if (FindFileData.cFileName[0] != '.')            {                flag = 1;                strcpy(szFile, lpPath);                strcat(szFile, "\\");                strcat(szFile, FindFileData.cFileName);                CubeGetFileName(szFile, fileList,srtRootPathName);                strReletivePath = szFile;            }        }        else        {            if (flag ==1)            {                strcpy(szFile, lpPath);                strcat(szFile, "\\");                strcat(szFile, FindFileData.cFileName);                //CubeGetFileName(szFile, fileList, srtRootPathName);                strReletivePath = szFile;                int a = strReletivePath.find(srtRootPathName, 0);                a += strlen(srtRootPathName) + 1;                strReletivePath = strReletivePath.substr(a, strlen(szFile) - a);                //strReletivePath.append("\\");                //strReletivePath.append(FindFileData.cFileName);                fileList.push_back(strReletivePath);            }            else             {                fileList.push_back(FindFileData.cFileName);            }        }        if (!FindNextFileA(hFind, &FindFileData))                break;    }    FindClose(hFind);}__int64 filesize(const char *path){    _finddatai64_t filefind;    int   done = 0, handle;    __int64 fs = -1;    if ((handle = _findfirsti64(path, &filefind)) == -1) return -1;    if (!(_A_SUBDIR == (_A_SUBDIR & filefind.attrib)))    {        fs = filefind.size;    }    _findclose(handle);    return fs;}//比较两个文件是否相同bool CompareFile1(const char *pFileName1, const char *pFileName2){//  int sizeFile1 = filesize(pFileName1);//  int sizeFile2 = filesize(pFileName2);// //  if (sizeFile1 !=sizeFile2)//  {//      return FALSE;//  }    //_lseeki64    fstream file1, file2;    char ch1, ch2;    file1.open(pFileName1, std::ios::binary | ios::in);    file2.open(pFileName2, std::ios::binary | ios::in);    if (!(file1.is_open() && file2.is_open()))    {        cout << "打开文件失败!" << endl;              return FALSE;    }    file1.getloc();    while (file1.get(ch1) && file2.get(ch2))    {        if (ch1 != ch2)        {            file1.close();            file2.close();            return FALSE;        }    }    if (!file1.get(ch1) && !file2.get(ch2))    {        file1.close();        file2.close();        return TRUE;    }    file1.close();    file2.close();    return FALSE;}BOOL compareFile3(char *pFile1, char *pFile2){    HANDLE FileHandle1;    HANDLE FileHandle2;    FileHandle1 = CreateFileA(pFile1, GENERIC_READ, NULL, NULL, OPEN_EXISTING,        FILE_ATTRIBUTE_NORMAL, NULL);    FileHandle2 = CreateFileA(pFile2, GENERIC_READ, NULL, NULL, OPEN_EXISTING,        FILE_ATTRIBUTE_NORMAL, NULL);    if (FileHandle1 == INVALID_HANDLE_VALUE || FileHandle2 == INVALID_HANDLE_VALUE)    {        CloseHandle(FileHandle1);        //一定注意在函数退出之前对句柄进行释放。        CloseHandle(FileHandle2);        return FALSE;    }    //大小比较    DWORD fileSize1 = SetFilePointer(FileHandle1, 0, NULL, FILE_END);  //从文件末尾移动 0 个字节以得到文件的长度    DWORD fileSize2 = SetFilePointer(FileHandle2, 0, NULL, FILE_END);  //从文件末尾移动 0 个字节以得到文件的长度    if (fileSize1 != fileSize2)    {        CloseHandle(FileHandle1);           CloseHandle(FileHandle2);        return FALSE;    }    //字节比较    for (LONGLONG filesize=1;filesize<fileSize1; filesize+= 256)    {        DWORD nByte1;        char buffer1[256] = {};        SetFilePointer(FileHandle1, filesize, NULL, FILE_BEGIN);        ReadFile(FileHandle1, buffer1, 256, &nByte1, NULL);             buffer1[nByte1];        DWORD nByte2;        char buffer2[256] = {};        SetFilePointer(FileHandle2, filesize, NULL, FILE_BEGIN);        ReadFile(FileHandle2, buffer2, 256, &nByte1, NULL);        buffer2[nByte2];        //memcmp(buffer1,buffer2,4);        if (memcmp(buffer1, buffer2, 256)!=0)        {            CloseHandle(FileHandle1);            CloseHandle(FileHandle2);            return FALSE;        }    }    CloseHandle(FileHandle1);    CloseHandle(FileHandle2);    return TRUE;}void CubeGetDiferetFileName(std::vector<string> &vec, char *newPath, char *olderPath, std::vector<string> &fileList){    if (vec.size() == NULL||newPath==NULL||        olderPath == NULL)        return;    for(std::vector<string>::iterator it= vec.begin(); it !=vec.end(); it++)    {        char newFilePath[MAX_PATH] = "";//这里没有将newFilePath定义为string类型。        char oldFilePath[MAX_PATH] = "";//因为下面stacat要求char*,string.c_str()为const char*        strcat(newFilePath, newPath);        strcat(newFilePath, it->c_str());        strcat(oldFilePath, olderPath);        strcat(oldFilePath, it->c_str());        if (PathFileExistsA(oldFilePath))        {            //同名文件存在,进行文件比较            //bool a = CompareFile1(newFilePath, oldFilePath);            bool a = compareFile3(newFilePath, oldFilePath);            if (a == TRUE)            {                //文件相同,忽略。判断下一个文件                continue;            }            else            {                //文件不同,记录该文件名                fileList.push_back(newFilePath);                //cout << newFilePath << endl;            }        }        else        {            //同名文件不存在,将newpath输出至vec            fileList.push_back(newFilePath);            //cout << newFilePath << endl;        }    }}int main(){    std::vector<string> vec;//保存新版本文件夹下文件名    std::vector<string> vec2;//不同的文件名    char *srtRootPathName = "versiona";    CubeGetFileName("C:\\Users\\CubeJoy\\Desktop\\versiona\\", vec, srtRootPathName);    CubeGetDiferetFileName(vec, "C:\\Users\\CubeJoy\\Desktop\\versiona\\",        "C:\\Users\\CubeJoy\\Desktop\\versionb\\", vec2);    system("pause");    return 0;}

原创粉丝点击