Windows下遍历文件目录

本文详细介绍了使用C++遍历文件目录的方法,包括使用FindFirstFile和FindNextFile函数的基本语法,并通过递归函数展示了如何深入目录结构进行检索。重点突出递归在目录遍历中的应用。

最近用到遍历文件目录,总结一下:

#include <Windows.h>

WIN32_FIND_DATA fndData;
HANDLE hFnd = INVALID_HANDLE_VALUE;

hFnd = ::FindFirstFile(_T("D:\\Program Files\\*.*"), &fndData);
if (hFnd == INVALID_HANDLE_VALUE)
{
    return ;
}

while (::FindNextFile(&fndData))
{
    CString strFileName = fndData.cFileName;
    if (fndData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
    {
		// 目录
    }
    else
    {
		// 文件
    }
}

if (::GetLastError() == 18)
{
    // 目录下的文件检索完毕.
}

::FindClose(hFnd);


我们可以通过一个递归函数来递归遍历:

void BuildRegSystem(
    const tstring& strFileSystemPath,
    const tstring& strAddParPath,
    CSysMgr& mgr
    )
{
    tstring strFsPath = strFileSystemPath;
    tstring strEnumPath = strFileSystemPath;
    tstring strAddNewPath = strAddParPath;

    if (*strFsPath.rbegin() != _T('\\'))
    {
        strFsPath += _T('\\');
    }

    if (*strAddNewPath.rbegin() != _T('\\'))
    {
        strAddNewPath += _T('\\');
    }

    strEnumPath = strFsPath + _T("*.*");

    WIN32_FIND_DATA fndFile;
    HANDLE hFnd = ::FindFirstFile(strEnumPath.c_str(), &fndFile);
    if (hFnd == INVALID_HANDLE_VALUE)
    {
        return ;
    }

    while (::FindNextFile(hFnd, &fndFile))
    {
        tstring strFileName = fndFile.cFileName;
        if (strFileName == _T(".."))
        {
			// 如果是上级目录,则不管.
            continue;
        }

        if (fndFile.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
        {
            // 如果是文件夹,则进行递归.
            mgr.CreateRegDirectory(strAddNewPath + strFileName);
            BuildRegSystem(
                strFsPath + strFileName,
                strAddNewPath + strFileName,
                regSystem
                );
        }
        else
        {
            mgr.InsertEntry(strAddParPath, strFileName);
        }
    }

    ::FindClose(hFnd);
}


void Test()
{
    CSysMgr mgr;

    BuildRegSystem(_T("D:\\Program Files\\11game"), _T("\\"), mgr);
    
    // ...
    // ...
}

没有任何复杂的操作,只为备忘。






Windows 命令行中遍历指定目录下的所有文件并查找包含特定关键字的文件,可以使用 `for` 命令结合 `findstr` 命令来实现。以下是一个完整的命令示例: ```cmd for %f in (*.txt) do findstr /m "your_keyword" "%f" && echo File %f contains the keyword ``` ### 说明: - `for %f in (*.txt)`:遍历当前目录下所有 `.txt` 文件。如果想遍历所有类型的文件,可以将 `*.txt` 替换为 `*.*` [^1]。 - `findstr /m "your_keyword" "%f"`:在每个文件中查找关键字 `your_keyword`,`/m` 参数表示只输出包含匹配字符串的文件名。 - `&& echo File %f contains the keyword`:如果找到了关键字,输出提示信息。 ### 查找所有类型文件中的关键字 如果希望在整个目录中查找所有类型文件中包含的关键字,可以使用以下命令: ```cmd for %f in (*.*) do findstr /m "your_keyword" "%f" && echo File %f contains the keyword ``` ### 在批处理脚本中使用 如果在批处理脚本中使用,需要将 `%f` 改为 `%%f`: ```cmd for %%f in (*.txt) do findstr /m "your_keyword" "%%f" && echo File %%f contains the keyword ``` ### 查找关键字并输出匹配行 如果不仅想知道哪些文件包含关键字,还想看到具体的匹配行,可以去掉 `/m` 参数: ```cmd for %f in (*.txt) do findstr "your_keyword" "%f" && echo File %f contains the keyword ``` ### 注意事项 - 在 Windows 的 `cmd.exe` 中,单引号 `'` 不会被识别为引号,应使用双引号 `"` 来包裹文件名或关键字 [^2]。 - 如果关键字中包含特殊字符(如 `.`、`*`、`?` 等),可以使用 `findstr /c:"your_keyword"` 来确保关键字被当作字符串处理。 ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值