简体   繁体   English

打印给定路径中的所有文件和子目录

[英]print all files and subdirectories in a give path

So, im writing a program to recursively print the directories/sub-directories and files in a given path. 因此,我正在编写程序以递归方式打印给定路径中的目录/子目录和文件。 im able to go in the first sub-directory and print all the files in it. 我能够进入第一个子目录并打印其中的所有文件。 my problem right now is i need find a way to step back one directory level and continue from where i left off reading. 我现在的问题是我需要找到一种方法来退回一个目录级别并从我停止阅读的地方继续。 Until the condition occurs at the original directory level. 直到条件发生在原始目录级别。

#include "Everything.h"
#include "Strsafe.h"

WIN32_FIND_DATA ffd;
HANDLE          hFind = INVALID_HANDLE_VALUE;
LARGE_INTEGER   fileSize;
DWORD           dwError;

void showdir(TCHAR *szDir);

int _tmain(int argc, LPCTSTR argv[])
{


    TCHAR           szDir[MAX_PATH];
    size_t          lengthOfArg;

    // verify number of parameters
    if (argc != 2)
    {
        ReportError(_T("Error: Incorrect number of arguments"), 1, FALSE);
    }

    // get the length of the entered directory
    StringCchLength(argv[1], MAX_PATH, &lengthOfArg);

    // verify that the directory path is not too long
    if (lengthOfArg > MAX_PATH - 2)
    {
        ReportError(_T("Error: Directory too long"), 2, FALSE);
    }

    // attach an asterisk (wildcard search char) to end of directory path
    StringCchCopy(szDir, MAX_PATH, argv[1]);
    StringCchCat(szDir, MAX_PATH, _T("*"));

    showdir(szDir);
}

void showdir(TCHAR *szDir)
{
    // begin the search; find the first file in the directory
    hFind = FindFirstFile(szDir, &ffd);
    if (hFind == INVALID_HANDLE_VALUE)
    {
        ReportError(_T("Error in searching"), 3, TRUE);
    }

    //hFind = FindFirstFile(szDir, &ffd);
    while (FindNextFile(hFind, &ffd) != 0)
    {
        if ((ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0)
        {
            fileSize.LowPart = ffd.nFileSizeLow;
            fileSize.HighPart = ffd.nFileSizeHigh;
            _tprintf(_T("%s   %ld\n"), ffd.cFileName, fileSize.QuadPart);

        }       

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

        if ((ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) 
            && (_tcscmp(ffd.cFileName, _T(".")) != 0 && (_tcscmp(ffd.cFileName, _T("..")) != 0)))
        {
            TCHAR fullpath[MAX_PATH];

            StringCchCopy(fullpath, strlen(szDir) - 0, szDir);
            StringCchCat(fullpath, MAX_PATH, ffd.cFileName);
            StringCchCat(fullpath, MAX_PATH, "\\");
            _tprintf(_T("<DIR>  %s \n"), fullpath);
            StringCchCat(fullpath, MAX_PATH, _T("*"));



            showdir(fullpath);

        }   
        // continue the search; try to find more files
    } 

    // figure out if we encountered an error other than "no more files"
    dwError = GetLastError();

    if (dwError != ERROR_NO_MORE_FILES)
    {
        ReportError(_T("Error in searching"), 4, TRUE);
    }

    FindClose(hFind);

}

Your global variables 您的全局变量

WIN32_FIND_DATA ffd;
HANDLE          hFind = INVALID_HANDLE_VALUE;
LARGE_INTEGER   fileSize;
DWORD           dwError;

should all be local variables of showdir() . 应该全部是showdir() 局部变量 Then each recursion level has its own search handle, and when a nested showdir() returns, the calling showdir() can simply continue enumerating its directory. 然后,每个递归级别都有其自己的搜索句柄,并且当嵌套的showdir()返回时,调用showdir()可以继续枚举其目录。

Note also that your code ignores the first file in each directory (the result of FindFirstFile() ). 还要注意,您的代码将忽略每个目录中的第一个文件( FindFirstFile()的结果)。 You could it rewrite as (error checking omitted for brevity): 您可以将其重写为(为简洁起见,省略了错误检查):

hFind = FindFirstFile(szDir, &ffd);
do {

    // ... handle ffd ...

} while (FindNextFile(hFind, &ffd))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM