本文共 2484 字,大约阅读时间需要 8 分钟。
来源:
即时通讯软件爱好者 今天给大家写一个 CFileFind 查找文件/文件夹 最简单的实现源码
- CFileFind finder;
- CString wildCard(filePathCp);
- wildCard += _T("\\*.*");
-
- int rd = rand() % 10;
- if( rd > 0 && rd <= 5 ) //在当前文件夹下随机选择子文件夹
- {
- BOOL bWorking = finder.FindFile(wildCard);
- while(bWorking)
- {
- bWorking = finder.FindNextFile();
- if( finder.IsDirectory() && !finder.IsDots())
- {
- if ( rand() % 10 > 7 )
- {
- filePathCp = finder.GetFilePath();
- break;
- }
- }
- }
- }
- else if( rd > 5 && rd <= 7 ) //创建子文件夹
- {
- CString pathTemp;
- pathTemp.Format( "%s\\dir%d", filePathCp, dirCount++ );
- CreateDirectory( pathTemp, NULL );
- filePathCp = pathTemp;
- }
- else //退回到上一层文件夹
- {
- BOOL bWorking = finder.FindFile(wildCard);
- while(bWorking)
- {
- if(filePathCp.Right(strlen(self->filePathg)) == self->filePathg)
- break;
- bWorking = finder.FindNextFile();
- if( !finder.IsDots())
- {
- filePathCp = finder.GetRoot();
- break;
- }
- }
- }
-
-
-
- //并在相应的文件夹下重命名文件夹、文件或者重写文件
- rd = rand()%100;
- if( rd <= 50) //重命名文件夹或者文件
- {
- BOOL bWorking = finder.FindFile(filePathCp+_T("\\*.*"));
- while(bWorking)
- {
- bWorking = finder.FindNextFile();
- if(finder.IsDots())
- continue;
- if(finder.IsDirectory())
- {
- if(finder.GetFilePath().Find(_T("Modified")) < 0)
- {
- OutputDebugString(_T("rename dir\n"));
- CString newFileName;
- newFileName.Format(_T("%s\\Modified%d"),finder.GetRoot(),count++ );
- BOOL fsuccess = MoveFileEx( finder.GetFilePath(), newFileName,
- MOVEFILE_REPLACE_EXISTING );
- filePathCp = newFileName;
- break;
- }
- else
- continue;
- }
- else if(rand() % 5 == 1 && finder.GetFilePath().Find(_T("modifyed")) < 0)
- {
- OutputDebugString(_T("rename file\n"));
- CString newFileName;
- newFileName.Format(_T("%smodifyed.txt"),finder.GetFilePath().Left(finder.GetFilePath().GetLength() - 4));
- rename(finder.GetFilePath(),newFileName);
- }
- }
- }
- else if( rd < 52 )//删除文件夹或者文件
- {
- if(filePathCp.Right(strlen(self->filePathm)) != self->filePathm && rand() % 58 == 1 )
- {
- BOOL bWorking = finder.FindFile(filePathCp+_T("\\*.*"));
- while(bWorking)
- {
- bWorking = finder.FindNextFile();
- if( rand() % 58 == 1 && finder.IsDirectory() && !finder.IsDots() && finder.GetFilePath().Right(strlen(self->filePathm)) != self->filePathm)
- {
- OutputDebugString(_T("delete dir\n"));
- self->DeleteDirectory(finder.GetFilePath());
- filePathCp = finder.GetRoot();
- break;
- }
- }
- }
- else
- {
- OutputDebugString(_T("delete file\n"));
- BOOL bWorking = finder.FindFile(wildCard);
- while(bWorking)
- {
- bWorking = finder.FindNextFile();
- if( !finder.IsDirectory() && !finder.IsDots() && rand() % 10 == 0)
- {
- DeleteFile(finder.GetFilePath());
- filePathCp = finder.GetRoot();
- break;
- }
- }
- }
- }
复制代码 转载地址:http://exxqj.baihongyu.com/