Folder Compare

记:今日在家休息,一前同事拜托写一程序,比较两个文件夹,看目标文件夹下新增,删除了那些文件

代码如下:

   1:   
   2:  #include "stdafx.h"
   3:   
   4:  #include <Shlwapi.h>
   5:  #include <Windows.h>
   6:   
   7:  #include <algorithm>
   8:  #include <io.h>
   9:  #include <iostream>
  10:  #include <string>
  11:  #include <tchar.h>
  12:  #include <vector>
  13:  using namespace std;
  14:   
  15:  typedef std::vector<string> paths;
  16:   
  17:   
  18:  BOOL IsDirectory(LPCTSTR pszPath)
  19:  {
  20:      return PathIsDirectory(pszPath);
  21:  }
  22:   
  23:  void GetFiles(const TCHAR* sPath, const TCHAR* sRootFolder, paths& files, bool bIncudeSubDir = true)
  24:  {
  25:      struct _finddata_t c_file;
  26:      long hFile;
  27:   
  28:      string sCD = sPath;
  29:      
  30:      sCD += _T("//*");
  31:   
  32:      if ( (hFile = (long)_findfirst(sCD.c_str(), &c_file)) == -1L )
  33:          return ;
  34:      else
  35:      {
  36:          do
  37:          {
  38:              string sFullPath = sPath;
  39:              sFullPath += _T("//");
  40:              sFullPath += c_file.name;
  41:   
  42:              if (_tcscmp(c_file.name, ".") == 0 || _tcscmp(c_file.name, "..") == 0)
  43:                  continue;
  44:              else if (IsDirectory(sFullPath.c_str()) && bIncudeSubDir)
  45:              {
  46:                  GetFiles(sFullPath.c_str(), sRootFolder, files, bIncudeSubDir);
  47:              }
  48:              else
  49:              {
  50:                  char szOut[MAX_PATH] = "";
  51:   
  52:                  PathRelativePathTo(szOut,
  53:                      sRootFolder,
  54:                      FILE_ATTRIBUTE_DIRECTORY,
  55:                      sFullPath.c_str(),
  56:                      FILE_ATTRIBUTE_NORMAL);
  57:   
  58:                  files.push_back(szOut);
  59:              }
  60:          } while ( _findnext(hFile, &c_file) == 0 );
  61:          _findclose(hFile);
  62:      }
  63:   
  64:      return ;
  65:  }
  66:   
  67:  void DumpPaths(const TCHAR* pTitle, const paths _paths)
  68:  {
  69:  #ifdef _DEBUG
  70:      std::cout<<pTitle << _T(":") <<std::endl;
  71:   
  72:      for (paths::const_iterator it = _paths.begin(); it != _paths.end(); ++it)
  73:      {
  74:          std::cout<<*it<<std::endl;
  75:      }
  76:  #endif
  77:  }
  78:   
  79:  bool FolderCompare(const TCHAR* folderA, const TCHAR* folderB,  paths& newFiles, paths& missedFiles)
  80:  {
  81:      paths filesInA;
  82:      GetFiles(folderA, folderA, filesInA);
  83:   
  84:      DumpPaths(folderA, filesInA);
  85:   
  86:      paths filesInB;
  87:      GetFiles(folderB, folderB, filesInB);
  88:   
  89:      DumpPaths(folderB, filesInB);
  90:   
  91:      // find out the new files
  92:      for (paths::const_iterator itB = filesInB.begin(); itB != filesInB.end(); ++itB)
  93:      {
  94:          if (std::find(filesInA.begin(), filesInA.end(), *itB) == filesInA.end())    // Not found => A new one
  95:          {
  96:              newFiles.push_back(*itB);
  97:          }
  98:      }
  99:   
 100:      // find out the missed files
 101:      for (paths::const_iterator itA = filesInA.begin(); itA != filesInA.end(); ++itA)
 102:      {
 103:          if (std::find(filesInB.begin(), filesInB.end(), *itA) == filesInB.end())    // Not found => this one is missed
 104:          {
 105:              missedFiles.push_back(*itA);
 106:          }
 107:      }
 108:   
 109:      return true;
 110:  }
 111:   
 112:  int _tmain(int argc, _TCHAR* argv[])
 113:  {
 114:      paths newFiles, missedFiles;
 115:      if (argc == 3)
 116:      {
 117:          FolderCompare(argv[1], argv[2], newFiles, missedFiles);
 118:      }
 119:      else
 120:          FolderCompare(_T("C://Jalen"), _T("C://Jalen - Copy"), newFiles, missedFiles);
 121:      
 122:      DumpPaths(_T("New files"), newFiles);
 123:   
 124:      DumpPaths(_T("Missed files"), missedFiles);
 125:   
 126:      char achar;
 127:      std::cin.get(achar);
 128:      return 0;
 129:  }
 
程序运行效果如下:
image
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值