CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Jul 2014
    Posts
    31

    Problem with CFileFind

    A fairly simple loop:

    Code:
    	if (rFinder.FindFile(strFolder + "\\*.*"))
    	{
    		while (rFinder.FindNextFile())
    		{
    			strPath = rFinder.GetFilePath();
    .
    .
    .
    .
                   }
            }
    What on earth would be causing this to only find directories - no files what so ever?

    I also noticed that, in another test directory I was using, CFileFind failed to find this sub-directory directory: "HTML Codes - Table of ascii characters and symbols_files". It did find the single HTML file that was also in this directory however.

    This class seems to be some what unreliable.

    Perhaps I would be better off using the old fashioned C version: findfirst and findnext.
    Last edited by gregaryb; August 4th, 2014 at 10:58 AM.

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,398

    Re: Problem with CFileFind

    What is rFinder?
    What is strFolder?
    What is strPath?

    Please, post the complete code for searching the files.
    Victor Nijegorodov

  3. #3
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,398

    Re: Problem with CFileFind

    Victor Nijegorodov

  4. #4
    Join Date
    Jul 2014
    Posts
    31

    Re: Problem with CFileFind

    void COtherFilesDialog::UploadFolder(CFileFind& rFinder, LPCSTR lpszFolder, LPCSTR lpszServerFolder)
    {
    CString strFolder = lpszFolder, strPath,
    strServerFolder = lpszServerFolder,
    strFolderSeperator = m_rIniFile.getKeyValue("FolderSeperator", "WebServer"),
    strCWD1, strCWD2;
    bool bIsTextFile = false;

    if (rFinder.FindFile(strFolder + "\\*.*"))
    {
    while (rFinder.FindNextFile())
    {
    strPath = rFinder.GetFilePath();
    if (rFinder.IsDots())
    {
    // Do nothing
    }
    else if (rFinder.IsDirectory())
    {
    CFileFind finder;
    CString strFolderName = rFinder.GetFileName();
    if (!((CEasyWebStoreUploaderDlg*)GetParent())->SetServerFolder(strFolderName))
    {
    ASSERT(((CEasyWebStoreUploaderDlg*)GetParent())->CreateServerFolder(strFolderName));
    ASSERT(((CEasyWebStoreUploaderDlg*)GetParent())->SetServerFolder(strFolderName));
    }
    UploadFolder(finder, strPath, strServerFolder + strFolderSeperator + strFolderName);
    ((CEasyWebStoreUploaderDlg*)GetParent())->GetServerFolder(strCWD1);
    ASSERT(((CEasyWebStoreUploaderDlg*)GetParent())->SetServerFolder(".."));
    ((CEasyWebStoreUploaderDlg*)GetParent())->GetServerFolder(strCWD2);
    }
    else
    {
    bIsTextFile = IsTextFile(rFinder.GetFilePath());
    ((CEasyWebStoreUploaderDlg*)GetParent())->UploadFile(strPath, rFinder.GetFileName(), "", bIsTextFile);
    }
    }
    }
    }

    void COtherFilesDialog::OnBnClickedButtonUploadWeb()
    {
    CFileFind finder;
    CString strPublicHTML;

    // if (ValidateWebFolder(GetWebFolder()))
    {
    strPublicHTML = m_rIniFile.getKeyValue("PublicHTML", "WebServer");
    ASSERT(((CEasyWebStoreUploaderDlg*)GetParent())->SetServerFolder(strPublicHTML));
    UploadFolder(finder, GetWebFolder(), strPublicHTML);
    }
    ((CEasyWebStoreUploaderDlg*)GetParent())->SetLastCtrlID(IDC_BUTTON_UPLOAD_WEB);
    }

  5. #5
    Join Date
    Jul 2014
    Posts
    31

    Re: Problem with CFileFind

    Quote Originally Posted by gregaryb View Post
    A fairly simple loop:

    Code:
    	if (rFinder.FindFile(strFolder + "\\*.*"))
    	{
    		while (rFinder.FindNextFile())
    		{
    			strPath = rFinder.GetFilePath();
    .
    .
    .
    .
                   }
            }
    What on earth would be causing this to only find directories - no files what so ever?

    I also noticed that, in another test directory I was using, CFileFind failed to find this sub-directory directory: "HTML Codes - Table of ascii characters and symbols_files". It did find the single HTML file that was also in this directory however.

    This class seems to be some what unreliable.

    Perhaps I would be better off using the old fashioned C version: findfirst and findnext.
    Code:
    void COtherFilesDialog::UploadFolder(CFileFind& rFinder, LPCSTR lpszFolder, LPCSTR lpszServerFolder)
    {
    	CString strFolder = lpszFolder, strPath, 
    			strServerFolder = lpszServerFolder,
    			strFolderSeperator = m_rIniFile.getKeyValue("FolderSeperator", "WebServer"),
    			strCWD1, strCWD2;
    	bool bIsTextFile = false;
    
    	if (rFinder.FindFile(strFolder + "\\*.*"))
    	{
    		while (rFinder.FindNextFile())
    		{
    			strPath = rFinder.GetFilePath();
     			if (rFinder.IsDots())
    			{
    				// Do nothing
    			}
    			else if (rFinder.IsDirectory())
    			{
    				CFileFind finder;
    				CString strFolderName = rFinder.GetFileName();
    				if (!((CEasyWebStoreUploaderDlg*)GetParent())->SetServerFolder(strFolderName))
    				{
    					ASSERT(((CEasyWebStoreUploaderDlg*)GetParent())->CreateServerFolder(strFolderName));
    					ASSERT(((CEasyWebStoreUploaderDlg*)GetParent())->SetServerFolder(strFolderName));
    				}
    				UploadFolder(finder, strPath, strServerFolder + strFolderSeperator + strFolderName);
    				((CEasyWebStoreUploaderDlg*)GetParent())->GetServerFolder(strCWD1);
    				ASSERT(((CEasyWebStoreUploaderDlg*)GetParent())->SetServerFolder(".."));
    				((CEasyWebStoreUploaderDlg*)GetParent())->GetServerFolder(strCWD2);
    			}
    			else
    			{
    				bIsTextFile = IsTextFile(rFinder.GetFilePath());
    				((CEasyWebStoreUploaderDlg*)GetParent())->UploadFile(strPath, rFinder.GetFileName(), "", bIsTextFile);
    			}
    		}
    	}
    }
    
    void COtherFilesDialog::OnBnClickedButtonUploadWeb()
    {
    	CFileFind finder;
    	CString strPublicHTML;
    
    //	if (ValidateWebFolder(GetWebFolder()))
    	{
    		strPublicHTML = m_rIniFile.getKeyValue("PublicHTML", "WebServer");
    		ASSERT(((CEasyWebStoreUploaderDlg*)GetParent())->SetServerFolder(strPublicHTML));
    		UploadFolder(finder, GetWebFolder(), strPublicHTML);
    	}
    	((CEasyWebStoreUploaderDlg*)GetParent())->SetLastCtrlID(IDC_BUTTON_UPLOAD_WEB);
    }

  6. #6
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Problem with CFileFind

    I believe he was asking what values those variables contain.

  7. #7
    Join Date
    Jul 2014
    Posts
    31

    Re: Problem with CFileFind

    I change the function to make it similar to the CFileFind example:

    Code:
    void COtherFilesDialog::UploadFolder(LPCSTR lpszFolder, LPCSTR lpszServerFolder)
    {
    	CString strFolder = lpszFolder, strPath, 
    			strServerFolder = lpszServerFolder,
    			strFolderSeperator = m_rIniFile.getKeyValue("FolderSeperator", "WebServer"),
    			strCWD1, strCWD2;
    	bool bIsTextFile = false;
    	CFileFind finder;
    
    	strFolder += "\\*.*";
    	if (finder.FindFile(strFolder))
    	{
    		while (finder.FindNextFile())
    		{
    			strPath = finder.GetFilePath();
     			if (finder.IsDots())
    				continue;
    			else if (finder.IsDirectory())
    			{/*
    				CString strFolderName = finder.GetFileName();
    				if (!((CEasyWebStoreUploaderDlg*)GetParent())->SetServerFolder(strFolderName))
    				{
    					ASSERT(((CEasyWebStoreUploaderDlg*)GetParent())->CreateServerFolder(strFolderName));
    					ASSERT(((CEasyWebStoreUploaderDlg*)GetParent())->SetServerFolder(strFolderName));
    				}
    				UploadFolder(strPath, strServerFolder + strFolderSeperator + strFolderName);
    				((CEasyWebStoreUploaderDlg*)GetParent())->GetServerFolder(strCWD1);
    				ASSERT(((CEasyWebStoreUploaderDlg*)GetParent())->SetServerFolder(".."));
    				((CEasyWebStoreUploaderDlg*)GetParent())->GetServerFolder(strCWD2);
    				*/
    			}
    			else
    			{
    				bIsTextFile = IsTextFile(finder.GetFilePath());
    				((CEasyWebStoreUploaderDlg*)GetParent())->UploadFile(strPath, finder.GetFileName(), "", bIsTextFile);
    			}
    		}
    		finder.Close();
    	}
    }
    I am using it on the following directory structure:

    g:
    test
    links.txt
    test
    links.txt

    But it does not work properly. My function only finds the following '.', '..', 'test' and 'test'.

    It wont find any of the 'links.txt'

    if I set a break point in the else statement it is never hit:
    Code:
    else
    			{
    				bIsTextFile = IsTextFile(finder.GetFilePath());
    				((CEasyWebStoreUploaderDlg*)GetParent())->UploadFile(strPath, finder.GetFileName(), "", bIsTextFile);
    			}
    I don't get it.

  8. #8
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,398

    Re: Problem with CFileFind

    You are ignoring using _T() macro. Why? Is your build an ANSI one?
    Did you debug your code?
    Victor Nijegorodov

  9. #9
    Join Date
    Jul 2014
    Posts
    31

    Re: Problem with CFileFind

    Eventually figured out why the function was only finding directories but not files.

    This loop structure works as expected:

    Code:
    	if (finder.FindFile(strWildCard))
    	{
    		while (bWorking)
    		{
    			bWorking = finder.FindNextFile() == TRUE;
                            .
                            .
                     }
    }
    This loop structure screws it up:

    Code:
    	if (finder.FindFile(strWildCard))
    	{
    		while (finder.FindNextFile())
    		{
                            .
                            .
                     }
    }
    Buggered if I can comprehend how this loop structure screws up FindNextFile()

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured