user15142611
user15142611

Reputation:

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

I created m_pFileFind using CFtpFileFind class.

I tried to search all files with FindFile() by performing dynamic allocation, but 0 is returned and the search is not available.

I tried debugging by referring to Microsoft docs, but I couldn't figure out the cause.

Please give me detailed advice.

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();
    }
}

Upvotes: 1

Views: 352

Answers (1)

Andrew Truckle
Andrew Truckle

Reputation: 19207

Please check the documentation for CFtpFileFind::FindNextFile where it states:

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.

In your code you have:

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

    // Snipped
}

You are never updating the bWorking variable inside the loop. Change it to:

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();
}

Upvotes: 1

Related Questions