博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
NanShan CFileFind 查找文件/文件夹 最简单的实现源码
阅读量:3517 次
发布时间:2019-05-20

本文共 2484 字,大约阅读时间需要 8 分钟。

来源:

即时通讯软件爱好者 今天给大家写一个 CFileFind 查找文件/文件夹 最简单的实现源码

  1. CFileFind finder;  
  2. CString wildCard(filePathCp);
  3. wildCard += _T("\\*.*");  
  4. int rd = rand() % 10;
  5.    if( rd > 0 && rd <= 5 )     //在当前文件夹下随机选择子文件夹
  6.    {
  7.     BOOL bWorking = finder.FindFile(wildCard);
  8.     while(bWorking)
  9.     {
  10.      bWorking = finder.FindNextFile();
  11.      if( finder.IsDirectory()  && !finder.IsDots())
  12.      {
  13.       if ( rand() % 10 > 7 )
  14.       {  
  15.        filePathCp = finder.GetFilePath();
  16.        break;
  17.       }
  18.      }
  19.     }
  20.    }
  21.    else if( rd > 5 && rd <= 7 ) //创建子文件夹
  22.    {
  23.     CString pathTemp;
  24.     pathTemp.Format( "%s\\dir%d", filePathCp, dirCount++ );
  25.     CreateDirectory( pathTemp, NULL );
  26.     filePathCp = pathTemp;
  27.    }
  28.    else                         //退回到上一层文件夹
  29.    {
  30.     BOOL bWorking = finder.FindFile(wildCard);
  31.     while(bWorking)
  32.     {
  33.      if(filePathCp.Right(strlen(self->filePathg)) == self->filePathg)
  34.       break;
  35.      bWorking = finder.FindNextFile();
  36.      if( !finder.IsDots())
  37.      {
  38.       filePathCp = finder.GetRoot();
  39.       break;
  40.      }
  41.     }
  42.    }
  43.    //并在相应的文件夹下重命名文件夹、文件或者重写文件
  44.    rd = rand()%100;
  45.    if( rd <= 50) //重命名文件夹或者文件
  46.    {
  47.     BOOL bWorking = finder.FindFile(filePathCp+_T("\\*.*"));
  48.     while(bWorking)
  49.     {
  50.      bWorking = finder.FindNextFile();
  51.      if(finder.IsDots())
  52.       continue;
  53.      if(finder.IsDirectory())
  54.      {
  55.       if(finder.GetFilePath().Find(_T("Modified")) < 0)
  56.       {
  57.        OutputDebugString(_T("rename dir\n"));
  58.        CString newFileName;
  59.        newFileName.Format(_T("%s\\Modified%d"),finder.GetRoot(),count++ );
  60.        BOOL fsuccess = MoveFileEx( finder.GetFilePath(), newFileName,
  61.         MOVEFILE_REPLACE_EXISTING );
  62.        filePathCp = newFileName;
  63.        break;
  64.       }
  65.       else
  66.        continue;
  67.      }
  68.      else if(rand() % 5 == 1 && finder.GetFilePath().Find(_T("modifyed")) < 0)
  69.      {
  70.       OutputDebugString(_T("rename file\n"));
  71.       CString newFileName;
  72.       newFileName.Format(_T("%smodifyed.txt"),finder.GetFilePath().Left(finder.GetFilePath().GetLength() - 4));
  73.       rename(finder.GetFilePath(),newFileName);
  74.      }
  75.     }
  76.    }
  77.    else if( rd < 52 )//删除文件夹或者文件
  78.    {
  79.     if(filePathCp.Right(strlen(self->filePathm)) != self->filePathm && rand() % 58 == 1 )
  80.     {
  81.      BOOL bWorking = finder.FindFile(filePathCp+_T("\\*.*"));
  82.      while(bWorking)
  83.      {
  84.       bWorking = finder.FindNextFile();
  85.       if( rand() % 58 == 1 && finder.IsDirectory() && !finder.IsDots() && finder.GetFilePath().Right(strlen(self->filePathm)) != self->filePathm)
  86.       {
  87.        OutputDebugString(_T("delete dir\n"));
  88.        self->DeleteDirectory(finder.GetFilePath());
  89.        filePathCp = finder.GetRoot();
  90.        break;
  91.       }
  92.      }
  93.     }
  94.     else
  95.     {
  96.      OutputDebugString(_T("delete file\n"));
  97.      BOOL bWorking = finder.FindFile(wildCard);
  98.      while(bWorking)
  99.      {
  100.       bWorking = finder.FindNextFile();
  101.       if( !finder.IsDirectory() && !finder.IsDots() && rand() % 10 == 0)
  102.       {
  103.        DeleteFile(finder.GetFilePath());
  104.        filePathCp = finder.GetRoot();
  105.        break;
  106.       }
  107.      }
  108.     }
  109.    }
复制代码

转载地址:http://exxqj.baihongyu.com/

你可能感兴趣的文章
前端知识体系
查看>>
查找入职员工时间排名倒数第三的员工所有信息
查看>>
使用join查询方式找出没有分类的电影id以及名称
查看>>
Qt教程(2) : Qt元对象系统
查看>>
驱动开发误用指针错误:Unable to handle kernel NULL pointer dereference at virtual address
查看>>
Linux部署DocSystem知识/文件管理系统
查看>>
Centos7开机自启动脚本无法使用备用方案
查看>>
jvm虚拟机内存详解
查看>>
线程的创建方式
查看>>
DNS是什么
查看>>
mapreduce自定义分组、自定义分区、二次排序
查看>>
Hbase架构
查看>>
spark运行模式
查看>>
PaddleX的C++使用
查看>>
MyBatis-Plus代码生成器
查看>>
我的第一个SpringBoot项目(一)
查看>>
回文数
查看>>
伪背包问题!
查看>>
求10000以内n的阶乘!
查看>>
static关键字
查看>>