English 中文(简体)
视窗C的反向名录如何回收
原标题:How to recursively traverse directories in C on Windows

最后,我想通过翻一番的档案和分局来旅行,并向所有档案撰写一些内容,而我发现这些档案具有某种延伸性(我的案件)。 何时,看一看我是否知道该项目是一份目录?

问题回答

这里是你们如何做的(这从记忆中都是如此,因此可能出现错误):

void FindFilesRecursively(LPCTSTR lpFolder, LPCTSTR lpFilePattern)
{
    TCHAR szFullPattern[MAX_PATH];
    WIN32_FIND_DATA FindFileData;
    HANDLE hFindFile;
    // first we are going to process any subdirectories
    PathCombine(szFullPattern, lpFolder, _T("*"));
    hFindFile = FindFirstFile(szFullPattern, &FindFileData);
    if(hFindFile != INVALID_HANDLE_VALUE)
    {
        do
        {
            if(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
            {
                // found a subdirectory; recurse into it
                PathCombine(szFullPattern, lpFolder, FindFileData.cFileName);
                FindFilesRecursively(szFullPattern, lpFilePattern);
            }
        } while(FindNextFile(hFindFile, &FindFileData));
        FindClose(hFindFile);
    }

    // Now we are going to look for the matching files
    PathCombine(szFullPattern, lpFolder, lpFilePattern);
    hFindFile = FindFirstFile(szFullPattern, &FindFileData);
    if(hFindFile != INVALID_HANDLE_VALUE)
    {
        do
        {
            if(!(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
            {
                // found a file; do something with it
                PathCombine(szFullPattern, lpFolder, FindFileData.cFileName);
                _tprintf_s(_T("%s
"), szFullPattern);
            }
        } while(FindNextFile(hFindFile, &FindFileData));
        FindClose(hFindFile);
    }
}

因此,你可以这样说。

FindFilesRecursively(_T("C:\WINDOWS"), _T("*.wav"));

在C:WINDOWS及其分局查找所有*.wav档案。

从技术上讲,你不需要做两件FindFirstFile(F)电话,但我发现Microsoft提供(即:MatchFileSpec)的配对功能与FindFirstFile(File)一样。 虽然“*.wav”可能罚款。

根据你对<代码>.wav的提及,Im将分析你重写Windows代码(似乎在*.wav文档中最为常见)。 在此情形下,请将<代码>FindFirstFile 和FindNextFile 用于反向名录。 这些使用<条码>WIN32_FIND_DATA结构,该结构有成员<条码>dwFileAttributes,其中载有标明档案属性的旗帜。 如果dwAttributes & FILE_ATTRIBUTE_DIRECTORY 是非零,你就有一个名录。

Very Helpful. I had anyway, a stack overflow since it was always adding "." to the path and returning to the same path = endless loop.

增加这一解决办法:

// found a subdirectory; recurse into it PathCombine(szFullPattern, lpFolder, FindFileData.cFileName); FindFilesRecursively(szFullPattern, lpPattern); if (FindFileData.cFileName[0] == . ) continue;

公开和读物(无)就是一个例子:

rel=“nofollow noreferer”>http://opengroup.org/onlinepubs/007908775/xsh/readdir.html

或窗户上的FindFirstFile

也可以非常容易地使用炮弹:

find . -name "*.wav"

or

ls **/*.wav    (in zsh and newer bashes)




相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签