如何使用 CFtpFileFind::FileFind() 方法搜索文件?

How to search for files with CFtpFileFind::FileFind() method?

我使用 CFtpFileFind class 创建了 m_pFileFind

我试图通过执行动态分配来搜索具有 FindFile() 的所有文件,但返回 0,并且搜索不可用。

参考微软文档进行了调试,没有找到原因。

请给我详细的建议

void CMFC_FTPDlg::ShowList()
{
    try
    {
        ConnectFTP();
        m_pFileFind = new CFtpFileFind(m_pConnection);

        m_pConnection->SetCurrentDirectory(_T("test"));
        m_pConnection->GetCurrentDirectory(m_strDir);
        MessageBox(m_strDir);
        
        BOOL bWorking = m_pFileFind->FindFile(_T("*"));
        while (bWorking)
        {
            // use finder.GetFileURL() as needed...
            m_pFileFind->FindNextFile();

            if (m_pFileFind->IsNormal())
            {
                //파일의 이름
                CString strfileName =  m_pFileFind->GetFileName();

                m_List.AddString(strfileName);   
            }
            else if (m_pFileFind->IsDirectory())
            {
                //파일의 이름
                CString strDireName = m_pFileFind->GetFileName();

                m_List.AddString(strDireName);   
            }
        }
    }
    catch(CInternetException* pEx)
    {
        pEx->ReportError(MB_ICONEXCLAMATION);
        m_pConnection = NULL;
        pEx->Delete();
    }
}

请查看 CFtpFileFind::FindNextFile 的文档,其中指出:

Return Value

Nonzero if there are more files; zero if the file found is the last one in the directory or if an error occurred. To get extended error information, call the Win32 function GetLastError. If the file found is the last file in the directory, or if no matching files can be found, the GetLastError function returns ERROR_NO_MORE_FILES.

在你的代码中你有:

BOOL bWorking = m_pFileFind->FindFile(_T("*"));
while (bWorking)
{
    // use finder.GetFileURL() as needed...
    m_pFileFind->FindNextFile();

    // Snipped
}

您永远不会更新循环内的 bWorking 变量。将其更改为:

BOOL bWorking = m_pFileFind->FindFile(_T("*"));
while (bWorking)
{
    // Work here on the file (since you have already found the first file before
    // starting the loop

    // Snipped

    bWorking = m_pFileFind->FindNextFile();
}