• XSS.stack #1 – первый литературный журнал от юзеров форума

[C++] Recursive File Search

plexus

RAID-массив
Пользователь
Регистрация
15.08.2020
Сообщения
59
Реакции
62
:) I needed some code for recursive filesearch and wanted something that didn't depend on any external libraries or headers besides the winapi so I wrote this:
C++:
#include <Windows.h>
#include <iostream>


void file_lister(std::wstring folder)
{
    std::wstring folder_path = folder + L"\\*";
    WIN32_FIND_DATA fd;

    HANDLE handle = FindFirstFile(folder_path.c_str(), &fd);
    if (handle == INVALID_HANDLE_VALUE)
        return;

    while (FindNextFile(handle, &fd)){       
        if (wcscmp(fd.cFileName, L".") == 0 || wcscmp(fd.cFileName, L"..") == 0)
            continue;

        std::wstring path = folder + L"\\" + std::wstring(fd.cFileName);
        std::wcout << path << std::endl;
        if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
            file_lister(path);
        }
    }
    FindClose(handle);
}

int main() {
    file_lister(L"C:\\Users");
    return 0;
}
It can be easily ported to C (just remove the use of iostream and change around a few of the datatypes) Feedback appreciated
 
Пожалуйста, обратите внимание, что пользователь заблокирован
Feedback appreciated
Well, it is a common and easy code, there is not much that can go wrong here. One thing I can think of on top of my head is that you should check if someone if trying to pass a folder path with a trailing slash to your function. Or use something like PathCombineW to combine paths.
 
Well, it is a common and easy code, there is not much that can go wrong here. One thing I can think of on top of my head is that you should check if someone if trying to pass a folder path with a trailing slash to your function. Or use something like PathCombineW to combine paths.
yeah i'll add that thank you :) Wanted to contribute some code snippets i have since I figured they might be of use to someone if they needed a good example without over relying on external stuff.
 
Пожалуйста, обратите внимание, что пользователь заблокирован
Код:
enum enumflags {
    ENUM_FILE = 1,
    ENUM_DIR,
    ENUM_BOTH
};

#include <io.h>
#include <functional>
bool enumsubfiles(

    const std::wstring& dir_with_back_slant,        //for example: L"C:\\", L"E:\\test\\"
    const std::wstring& filename,                   //for example: L"123.txt", L"*.exe", L"123.???"
    unsigned int maxdepth,                 //0 means not searching subdirectories, 1 means maximum depth of subdirectories is 1,
                                           //    pass -1 to search all the subdirectories.
    enumflags flags,                       //search files, directories, or both.
    std::function<bool(const std::wstring& dir_with_back_slant, _wfinddata_t& attrib)> callback
) {
    _wfinddata_t dat;
    size_t hfile;
    std::wstring fullname = dir_with_back_slant + filename;
    std::wstring tmp;
    bool ret = true;


    hfile = _wfindfirst(fullname.c_str(), &dat);
    if (hfile == -1) goto a;
    do {
        if (!(wcscmp(L".", dat.name) && wcscmp(L"..", dat.name))) continue;
        if (((dat.attrib & _A_SUBDIR) && (!(flags & ENUM_DIR))) || ((!(dat.attrib & _A_SUBDIR)) && (!(flags & ENUM_FILE)))) continue;
        ret = callback(dir_with_back_slant, dat);
        if (!ret) {
            _findclose(hfile);
            return ret;
        }
    } while (_wfindnext(hfile, &dat) == 0);
    _findclose(hfile);

a:

    if (!maxdepth) return ret;

    tmp = dir_with_back_slant + L"*";
    hfile = _wfindfirst(tmp.c_str(), &dat);
    if (hfile == -1) return ret;
    do {
        if (!(wcscmp(L".", dat.name) && wcscmp(L"..", dat.name))) continue;
        if (!(dat.attrib & _A_SUBDIR)) continue;
        tmp = dir_with_back_slant + dat.name + L"\\";
        ret = enumsubfiles(tmp, filename, maxdepth - 1, flags, callback);
        if (!ret) {
            _findclose(hfile);
            return ret;
        }


    } while (_wfindnext(hfile, &dat) == 0);
    _findclose(hfile);

    return ret;

}
Usage:
Код:
bool x=enumsubfiles(startFolder, L"*.*", -1, ENUM_FILE, [](const std::wstring& dir_with_back_slant, _wfinddata_t& attrib)->bool
        {

            std::wcout<<dir_with_back_slant<<std::endl;
            }

            return true;          //return true to continue, return false to abort searching.
        });
 
Последнее редактирование:


Напишите ответ...
  • Вставить:
Прикрепить файлы
Верх