c语言 遍历 磁盘文件,C++非递归遍历磁盘文件和递归遍历磁盘文件的程序示例

// File Name: CSearch.cpp

#include "stdafx.h"

#include "CSearch.h"

#include

#pragma comment(lib, "Shell32.lib")

#include

Search::Search()

{

}

Search::~Search()

{

}

void Search::Start(void)

{

char buffer[MAX_PATH] = {0};

::SHGetSpecialFolderPathA(NULL, buffer, CSIDL_WINDOWS, FALSE);

CString strPath(buffer);

strPath += _T("\\RTconfig.ini");

if (!PathFileExists(strPath))

{

if (PathFileExists(_T("RTconfig.ini")))

{

MoveFile(_T("RTconfig.ini"), strPath);

}

else

{

return;

}

}

CStdioFile file;

if (file.Open(strPath, CFile::modeRead))

{

char* old_locale=_strdup(setlocale(LC_CTYPE,NULL) );

setlocale( LC_CTYPE,"chs");

CString strBuffer;

while(file.ReadString(strBuffer))

{

m_strSearchName.push_back(strBuffer);

}

setlocale( LC_CTYPE, old_locale ); //还原语言区域的设置

free( old_locale );//还原区域设定

file.Close();

}

TCHAR strBuffer[50] = {0};

TCHAR * pStr = strBuffer;

CString strTempName;

// 获取磁盘驱动器

GetLogicalDriveStrings(50, strBuffer);

strTempName = strBuffer;

while (strTempName != _T(""))

{

// 如果是磁盘号

if (DRIVE_FIXED == GetDriveType(strTempName))

{

strPathStack.push(strTempName);

}

while(*pStr)

{

pStr++;

}

pStr++;

strTempName = pStr;

}

CString strTemp;

while (!strPathStack.empty())

{

strTemp = strPathStack.top();

strPathStack.pop();

ListAllFileInDrectory(strTemp);

}

}

void Search::ListAllFileInDrectory(CString strPath)

{

WIN32_FIND_DATA FindFileData;

HANDLE hListFile;

hListFile = FindFirstFile(strPath + _T("\\*.*"), &FindFileData);

if (hListFile == INVALID_HANDLE_VALUE)

{

//"错误码:" GetLastError()

}

else

{

do

{

// 过滤"."和".."

CString strTemp(FindFileData.cFileName);

if (strTemp == _T(".") || strTemp == _T(".."))

{

continue;

}

strTemp = FindFileData.cFileName;

strTemp.MakeLower();

if (-1 != strTemp.Find(_T(".txt")) || -1 != strTemp.Find(_T(".doc")) || -1 != strTemp.Find(_T(".docx")))

{

std::vector::iterator iter;

for (iter = m_strSearchName.begin(); iter != m_strSearchName.end(); iter++)

{

if (-1 != strTemp.Find((*iter).MakeLower()))

{

m_strPath.push_back(strPath + _T("\\") + FindFileData.cFileName);

break;        // 跳出循环

}

}

}

// 如果是目录 且不是系统属性目录 压栈

if (FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY && !(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_SYSTEM))

{

strPathStack.push(strPath + _T("\\") + FindFileData.cFileName);

}

}

while(FindNextFile(hListFile, &FindFileData));

}

FindClose(hListFile);            // 关闭句柄,不然造成内存溢出

}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值