循环遍历目录并打印子文件的文件和内容

时间:2015-10-27 00:25:31

标签: c windows while-loop directory

注意:不能使用dirent.h库。

我需要修改程序以打印目录中的文件和子目录。任何帮助将不胜感激。

例如,这就是输出的样子:

<DIR> U:\Test\
   sample (2).txt   6
   sample.txt   6

<DIR> U:\Test\New folder (5)\
   sample (4).txt   6
   sample (3).txt   6
   sample (2).txt   6
   sample.txt   6

<DIR> U:\Test\New folder (4)\
   sample (5).txt   6
   sample (4).txt   6
   sample (3).txt   6
   sample (2).txt   6
   sample.txt   6

<DIR> U:\Test\New folder (3)\
   sample.txt   6

<DIR> U:\Test\New folder (2)\
   sample (4).txt   6
   sample (3).txt   6
   sample (2).txt   6
   sample.txt   6

<DIR> U:\Test\New folder\
   sample - Copy (2).txt   6
   sample - Copy.txt   6
   sample.txt   6

这是我到目前为止只列出文件和子目录的内容。我需要能够打开子目录并打印相关文件。我试图在单独的功能中搜索和打印文件。

WIN32_FIND_DATA ffd;
    HANDLE          hFind = INVALID_HANDLE_VALUE;
    size_t          lengthOfArg;
    TCHAR           szDir[MAX_PATH];
    LARGE_INTEGER   fileSize;
    DWORD           dwError;
    FILE            *fp;


do
    {
        // did we find a directory?
        // ffd.dwFileAttributes says this is a directory (FILE_ATTRIBUTE_DIRECTORY)
        if (ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)


            _tprintf(_T("%s  <DIR>\n"), ffd.cFileName);

        // did we find a file?
        else
        {
            fileSize.LowPart = ffd.nFileSizeLow;
            fileSize.HighPart = ffd.nFileSizeHigh;
            _tprintf(_T("%s   %ld\n"), ffd.cFileName, fileSize.QuadPart);
        }

        // continue the search; try to find more files
    } while (FindNextFile(hFind, &ffd) != 0);

注意:不能使用dirent.h库。

0 个答案:

没有答案