遍历目录文件路径(Win32, C++)

49 篇文章 0 订阅
5 篇文章 0 订阅

GetFileList.h


#pragma once

#define _AFXDLL
#include <iostream>
#include <vector>
#include <afx.h>
#include <atlstr.h>

#ifdef UNICODE
using _tstring = std::wstring;
#else
using _tstring = std::string;
#endif

//
// @brief: 获取文件扩展名
// @param: strfileName     文件名, 如: test.exe
// @param: bIncludeDot     是否包含 . 号
// @ret: 扩展名            如 test.exe bIncludeDot = false 时返回 exe, bIncludeDot = true 时返回 .exe, 
CString GetFileExtension(
    const CString& strfileName,
    bool bIncludeDot = false
);

//
// @brief: 获取目录下文件路径
// @param: fileList     文件路径字符串容器
// @param: dir          指定目录
// @param: extList      扩展名过滤列表, 如: {("exe"), ("txt")}
// @param: depth        目录深度 如: 0:指定目录为根目录, 遍历0层子目录 
// @ret: void
void GetFileList(
    std::vector<CString>& fileList,
    const CString& dir,
    const std::vector<CString>& extList = {},
    int depth = MAX_PATH,
    bool bIgnoreCase = true);

//
// @brief: 获取文件扩展名
// @param: strfileName     文件名, 如: test.exe
// @param: bIncludeDot     是否包含 . 号
// @ret: 扩展名            如 test.exe bIncludeDot = false 时返回 exe, bIncludeDot = true 时返回 .exe, 
_tstring GetFileExtName(
    const _tstring& strfileName,
    bool bIncludeDot = false
);

//
// @brief: 获取目录下文件路径
// @param: fileList     文件路径字符串容器
// @param: dir          指定目录
// @param: extList      扩展名过滤列表, 如: {("exe"), ("txt")}
// @param: depth        目录深度 如: 0:指定目录为根目录, 遍历0层子目录 
// @ret: void
void GetFileList(
    std::vector<_tstring>& fileList,
    const _tstring& dir,
    const std::vector<_tstring>& extList = {},
    int32_t depth = MAX_PATH,
    bool bIgnoreCase = true
);

 

GetFileList.cpp


#include "GetFileList.h"
#include <tchar.h>

_tstring GetFileExtName(const _tstring& strfileName, bool bIncludeDot/* = false*/)
{
    _tstring strExtName = strfileName;
    size_t nExtPos = strExtName.find_last_of(_T('.'));
    if (_tstring::npos == nExtPos)
    {
        return _T("");
    }

    if (bIncludeDot)
    {
        strExtName.erase(0, nExtPos);
    }
    else
    {
        strExtName.erase(0, nExtPos + 1);
    }

    return strExtName;
}

void GetFileList(
    std::vector<_tstring>& fileList, 
    const _tstring& dir, 
    const std::vector<_tstring>& extList /* = {} */, 
    int32_t depth /*  = MAX_PATH */,
    bool bIgnoreCase/* = true*/
)
{
    //到达深度限制返回
    if (depth < 0)
    {
        return;
    }

    WIN32_FIND_DATA findData = { 0 };
    HANDLE hFindHandle = INVALID_HANDLE_VALUE;
    hFindHandle = FindFirstFile((dir + _T("\\*.*")).c_str(), &findData);
    if (INVALID_HANDLE_VALUE == hFindHandle)
    {
        return;
    }

    _tstring strName;
    do
    {
        strName = findData.cFileName;

        //非目录
        if (!(findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
        {
            if (extList.empty())
            {
                fileList.emplace_back(dir + _T("\\") + strName);
            }
            else
            {
                //获取扩展名
                _tstring strExtName = GetFileExtName(strName, false);

                //大小写比较区分
                if (bIgnoreCase)
                {
                    //进行扩展名过滤
                    for (auto& item : extList)
                    {
                        if (0 == _tcsicmp(item.c_str(), strExtName.c_str()))
                        {
                            fileList.emplace_back(dir + _T("\\") + strName);
                        }
                    }
                }
                else
                {
                    //进行扩展名过滤
                    for (auto& item : extList)
                    {
                        if (item == strExtName)
                        {
                            fileList.emplace_back(dir + _T("\\") + strName);
                        }
                    }
                }
            }
            continue;
        }

        //上一级目录与当前目录跳过
        if (0 == _tcscmp(findData.cFileName, _T(".")) || 0 == _tcscmp(findData.cFileName, _T("..")))
        {
            continue;
        }

        (void)GetFileList(fileList, dir + _T("\\") + strName, extList, depth - 1);

    } while (::FindNextFile(hFindHandle, &findData));

    ::FindClose(hFindHandle);
}

CString GetFileExtension(const CString& strfileName, bool bIncludeDot /* = false*/)
{
    if (strfileName == _T(""))
    {
        return _T("");
    }

    CString strFileExtension = strfileName;

    int i64Index = strFileExtension.ReverseFind(_T('.'));

    if (-1 == i64Index)
    {
        return _T("");
    }

    if (!bIncludeDot)
    {
        strFileExtension = strFileExtension.Mid(i64Index + 1);
    }
    else
    {
        strFileExtension = strFileExtension.Mid(i64Index);
    }

    return strFileExtension;
}

void GetFileList(
    std::vector<CString>& fileList, 
    const CString& dir, 
    const std::vector<CString>& extList/* = {}*/, 
    int depth/* = 1*/, 
    bool bIgnoreCase/* = true*/)
{
    if (depth < 0)
    {
        return;
    }

    CFileFind finder;
    // 总文件夹,开始遍历
    CString strFind = dir + _T("\\*.*");
    BOOL isNotEmpty = finder.FindFile(strFind);

    while (isNotEmpty)
    {
        isNotEmpty = finder.FindNextFile(); // 查找文件
        CString filename = finder.GetFilePath(); // 获取文件的路径,可能是文件夹,可能是文件
        if (!finder.IsDirectory())
        {
            // 如果是文件则加入文件列表
            CString fileExt = GetFileExtension(filename, false);

            if (extList.empty())
            {
                fileList.push_back(filename); //将一个文件路径加入容器
            }
            else
            {
                //大小写比较区分
                if (bIgnoreCase)
                {
                    for (auto& item : extList)
                    {
                        if (0 == item.CompareNoCase(fileExt))
                        {
                            fileList.push_back(filename); //将一个文件路径加入容器
                        }
                    }
                }
                else
                {
                    for (auto& item : extList)
                    {
                        if (item == fileExt)
                        {
                            fileList.push_back(filename); //将一个文件路径加入容器
                        }
                    }
                }
            }
        }
        else
        {
            // 递归遍历用户文件夹,跳过非用户文件夹 
            if (!(finder.IsDots()))
            {
                GetFileList(fileList, filename, extList, depth - 1, bIgnoreCase);
            }
        }
    }
}

 

main.cpp


#include "GetFileList.h"
#include <time.h>

int main()
{
    clock_t tBeg;
    clock_t tEnd;
    std::vector<_tstring> vFileList;

    vFileList.clear();
    tBeg = clock();
    GetFileList(vFileList, _T(R"(C:\Program Files (x86))"));
    tEnd = clock();
    printf("*.*   costTime: %dms, file count: %zd\n", tEnd - tBeg, vFileList.size());

    vFileList.clear();
    tBeg = clock();
    GetFileList(vFileList, _T(R"(C:\Program Files (x86))"), { _T("exe") });
    tEnd = clock();
    printf("*.exe costTime: %dms, file count: %zd\n", tEnd - tBeg, vFileList.size());

    vFileList.clear();
    tBeg = clock();
    GetFileList(vFileList, _T(R"(C:\Program Files (x86))"), { _T("txt") });
    tEnd = clock();
    printf("*.txt costTime: %dms, file count: %zd\n", tEnd - tBeg, vFileList.size());

    system("pause");

    return 0;
}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在信号处理领域,DOA(Direction of Arrival)估计是一项关键技术,主要用于确定多个信号源到达接收阵列的方向。本文将详细探讨三种ESPRIT(Estimation of Signal Parameters via Rotational Invariance Techniques)算法在DOA估计中的实现,以及它们在MATLAB环境中的具体应用。 ESPRIT算法是由Paul Kailath等人于1986年提出的,其核心思想是利用阵列数据的旋转不变性来估计信号源的角度。这种算法相比传统的 MUSIC(Multiple Signal Classification)算法具有较低的计算复杂度,且无需进行特征值分解,因此在实际应用中颇具优势。 1. 普通ESPRIT算法 普通ESPRIT算法分为两个主要步骤:构造等效旋转不变系统和估计角度。通过空间平移(如延时)构建两个子阵列,使得它们之间的关系具有旋转不变性。然后,通过对子阵列数据进行最小二乘拟合,可以得到信号源的角频率估计,进一步转换为DOA估计。 2. 常规ESPRIT算法实现 在描述中提到的`common_esprit_method1.m`和`common_esprit_method2.m`是两种不同的普通ESPRIT算法实现。它们可能在实现细节上略有差异,比如选择子阵列的方式、参数估计的策略等。MATLAB代码通常会包含预处理步骤(如数据归一化)、子阵列构造、旋转不变性矩阵的建立、最小二乘估计等部分。通过运行这两个文件,可以比较它们在估计精度和计算效率上的异同。 3. TLS_ESPRIT算法 TLS(Total Least Squares)ESPRIT是对普通ESPRIT的优化,它考虑了数据噪声的影响,提高了估计的稳健性。在TLS_ESPRIT算法中,不假设数据噪声是高斯白噪声,而是采用总最小二乘准则来拟合数据。这使得算法在噪声环境下表现更优。`TLS_esprit.m`文件应该包含了TLS_ESPRIT算法的完整实现,包括TLS估计的步骤和旋转不变性矩阵的改进处理。 在实际应用中,选择合适的ESPRIT变体取决于系统条件,例如噪声水平、信号质量以及计算资源。通过MATLAB实现,研究者和工程师可以方便地比较不同算法的效果,并根据需要进行调整和优化。同时,这些代码也为教学和学习DOA估计提供了一个直观的平台,有助于深入理解ESPRIT算法的工作原理。
以下是使用C语言在Windows遍历目录文件的示例代码: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <windows.h> void traverse_dir(const char *path); int main(int argc, char *argv[]) { if (argc != 2) { printf("Usage: %s <dir_path>\n", argv[0]); return 1; } traverse_dir(argv[1]); return 0; } void traverse_dir(const char *path) { char search_path[MAX_PATH]; WIN32_FIND_DATA file_data; sprintf(search_path, "%s\\*", path); HANDLE handle = FindFirstFile(search_path, &file_data); if (handle == INVALID_HANDLE_VALUE) { printf("Failed to open directory: %s\n", path); return; } do { if (strcmp(file_data.cFileName, ".") == 0 || strcmp(file_data.cFileName, "..") == 0) { continue; } if (file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { // 子目录 char sub_path[MAX_PATH]; sprintf(sub_path, "%s\\%s", path, file_data.cFileName); traverse_dir(sub_path); } else { // 文件 char file_path[MAX_PATH]; sprintf(file_path, "%s\\%s", path, file_data.cFileName); printf("%s\n", file_path); } } while (FindNextFile(handle, &file_data)); FindClose(handle); } ``` 在此代码中,我们使用了Windows API中的`FindFirstFile`和`FindNextFile`函数来遍历目录文件。具体实现步骤如下: 1. 将指定目录路径拼接成搜索路径,例如`C:\test\*`。 2. 调用`FindFirstFile`函数来打开目录并获取第一个文件目录的信息。 3. 如果打开目录失败,则退出遍历函数。 4. 如果当前文件目录是`.`或`..`,则跳过。 5. 如果当前文件目录目录,则递归调用遍历函数。 6. 如果当前文件目录文件,则打印文件路径。 7. 调用`FindNextFile`函数获取下一个文件目录的信息,重复步骤4-6,直到遍历完整个目录。 8. 调用`FindClose`函数关闭目录句柄。 需要注意的是,我们在拼接子目录路径文件路径时,使用了`sprintf`函数。为了避免缓冲区溢出,我们将缓冲区大小定义为`MAX_PATH`,这是一个Windows API中的常量,表示文件路径的最大长度。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值