Traversing directories recursivelly using FindFirstFileEx function [closed]

ε祈祈猫儿з 提交于 2020-01-16 03:57:10

问题


I've debugged it, but I still don't have any idea what could be the culprit.

#ifndef UNICODE 
#define UNICODE
#endif


#include <stdio.h>
#include <Windows.h>


void EndWithBackslash(TCHAR* string)
{
    if(string[wcslen(string)-1] != TEXT('\\')) wcscat(string,TEXT("\\"));
}


void Browse(const TCHAR* curdir)
{
    HANDLE hFoundFile;
    WIN32_FIND_DATA foundFileData;
    TCHAR buffer[MAX_PATH];

    wcscpy(buffer,curdir);

    EndWithBackslash(buffer);

    SetCurrentDirectory(buffer);

    hFoundFile = FindFirstFileEx(TEXT("*"),FINDEX_INFO_LEVELS::FindExInfoBasic,&foundFileData ,FINDEX_SEARCH_OPS::FindExSearchLimitToDirectories ,NULL , NULL);

    if(hFoundFile != INVALID_HANDLE_VALUE)
    {
        do
        {
            if ((foundFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) && wcscmp(foundFileData.cFileName,TEXT(".")) && wcscmp(foundFileData.cFileName,TEXT("..")) )
            {   
                EndWithBackslash(buffer);
                wcscat(buffer,foundFileData.cFileName);
                wprintf(TEXT("%s\n"),buffer);


                Browse(buffer);
            }
        }
        while(FindNextFile(hFoundFile,&foundFileData));
        FindClose(hFoundFile);


    }
}

int main(void) 
{   
    Browse(TEXT("F:\\"));

    system("Pause");
    return 0;
}

Output:

F:\$RECYCLE.BIN
F:\$RECYCLE.BIN\S-1-5-21-1271883188-2384997935-49719322-1000
F:\$RECYCLE.BIN\Seagate
F:\$RECYCLE.BIN\Seagate\Seagate Dashboard 2.0
F:\$RECYCLE.BIN\Seagate\Seagate Dashboard 2.0\System Volume Information
F:\$RECYCLE.BIN\Seagate\Seagate Dashboard 2.0\System Volume Information\Video

How does "the first layer" realy looks:

Could you point out my mistake(s)?


回答1:


The inner part should be:

            TCHAR pszItemPath[MAX_PATH];
            wcscpy(pszItemPath, buffer);
            // NOTE: Now when we took a copy of buffer, we don't touch it so that next iteration would have it good and untouched
            EndWithBackslash(pszItemPath);
            wcscat(pszItemPath, foundFileData.cFileName);
            wprintf(TEXT("%s\n"), pszItemPath);


来源:https://stackoverflow.com/questions/12334043/traversing-directories-recursivelly-using-findfirstfileex-function

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!