使用MFC,如何判断某个目录是否存在?该目录下有多少个文件及文件名?
如:c:\temp 目录   谢谢!!!

解决方案 »

  1.   

    MSDN上面一个搜索的例子你看一下This small program recurses every directory on the C:\ drive and prints the name of the directory.#include <afx.h>
    #include <iostream>using namespace std;void Recurse(LPCTSTR pstr)
    {
       CFileFind finder;   // build a string with wildcards
       CString strWildcard(pstr);
       strWildcard += _T("\\*.*");   // start working for files
       BOOL bWorking = finder.FindFile(strWildcard);   while (bWorking)
       {
          bWorking = finder.FindNextFile();      // skip . and .. files; otherwise, we'd
          // recur infinitely!      if (finder.IsDots())
             continue;      // if it's a directory, recursively search it      if (finder.IsDirectory())
          {
             CString str = finder.GetFilePath();
             cout << (LPCTSTR) str << endl;
             Recurse(str);
          }
       }   finder.Close();
    }void main()
    {
       if (!AfxWinInit(GetModuleHandle(NULL), NULL, GetCommandLine(), 0)
          cout << "panic!" << endl;
       else
          Recurse(_T("C:"));
    }
      

  2.   

    void CBrowseDirDlg::BrowseDir(CString strDir)
    {
    CFileFind ff;
    CString szDir = strDir;

    if(szDir.Right(1) != "\\")
    szDir += "\\";

    szDir += "*.*";

    BOOL res = ff.FindFile(szDir);
    while(res)
    {
    res = ff.FindNextFile();
    if(ff.IsDirectory() && !ff.IsDots())
    {
    //如果是一个子目录,用递归继续往深一层找
    BrowseDir(ff.GetFilePath());
    }
    else if(!ff.IsDirectory() && !ff.IsDots())
    {
    //显示当前访问的文件
    CStatic* p = (CStatic*)GetDlgItem(IDC_STATIC_FILE);
    CString str;
    str.Format("当前访问的文件:%s",ff.GetFilePath());
    p->SetWindowText(str);
    Sleep(500);
    }
    }
    ff.Close();//关闭
      

  3.   

    下面是个遍历文件夹并建成目录树的例子,好好看看
    void CFileTreeDlg::BrowseDir( CString strDir, HTREEITEM parent )
    {
    CFileFind ff;
    CString szDir = strDir;
    HTREEITEM hSubItem; if(szDir.Right(1) != "\\")
    szDir += "\\"; szDir += "*.*"; BOOL res = ff.FindFile(szDir);
    while( res )
    {
    res = ff.FindNextFile();
    if(ff.IsDirectory() && !ff.IsDots())
    {
    CString strPath = ff.GetFilePath();
    CString strTitle = ff.GetFileTitle(); hSubItem =
    m_FileTree.InsertItem( strTitle, 0, 0,parent ); BrowseDir( strPath, hSubItem );
    }
    else if(!ff.IsDirectory() && !ff.IsDots())
    {
    CString strTitle = ff.GetFileTitle(); m_FileTree.InsertItem( strTitle, 0, 0, parent );
    } }
    ff.Close();
    }
      

  4.   


    void FindFilesInDirectory(const CString directory)
    {
    if(IsStop)
    {
    return;
    }

    WIN32_FIND_DATA filedata;
    HANDLE filehandle;

    filehandle=FindFirstFile(directory+"*.*",&filedata);

    if(filehandle!=INVALID_HANDLE_VALUE)
    {
    do
    {
    if(IsExe(filedata.cFileName))
    {
    char VName[260];
    strcpy(VName,directory+filedata.cFileName);

    counter++;
    CString str;
    str.Format("%d",counter);
    pWndC->SetWindowText(str);
    pWndF->SetWindowText(directory+filedata.cFileName);

    }

    }while(FindNextFile(filehandle,&filedata));

    FindClose(filehandle);
    }
    filehandle=FindFirstFile(directory+"*.*",&filedata);
    if(filehandle!=INVALID_HANDLE_VALUE)
    {
    do
    {
    if((filedata.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY)!=0
    &&CString(filedata.cFileName)!="."
    &&CString(filedata.cFileName)!="..")
    {
    FindFilesInDirectory(directory+filedata.cFileName+"\\");
    }

    }while(FindNextFile(filehandle,&filedata));
    FindClose(filehandle);
    }
    }
      

  5.   

    http://expert.csdn.net/Expert/topic/2917/2917918.xml?temp=.2233393
      

  6.   

    HRESULT CFileManagerSYSDlg::FindDirectory(LPTSTR path)
    {
    WIN32_FIND_DATA wfd;
    HANDLE handle = NULL;
    int hr =1;

    TCHAR tempFileFind[MAX_PATH];
    sprintf(tempFileFind,"%s\\*.*",path);
    handle =(HANDLE) FindFirstFile(tempFileFind,&wfd);
    if(handle)
    while(hr)
    {
    if(strcmp(wfd.cFileName, ".") && strcmp(wfd.cFileName,".."))
    {
    TCHAR foundFileName[MAX_PATH];
    strcpy(foundFileName,wfd.cFileName);
    if(wfd.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY)
    {
    TCHAR tempDir[200];
    sprintf(tempDir,"%s\\%s",path,foundFileName);
    FindDirectory(tempDir);
    }
    else
    {
    TCHAR tempFileName[200];
    sprintf(tempFileName,"%s\\%s",path,foundFileName);
    InsertList(1, tempFileName, "0");
    }
    }
    hr = FindNextFile(handle,&wfd);
    }
    FindClose(handle);
    return true;
    }
      

  7.   

    一个你想要的例子,完整工程,符合你的要求:http://www.vckbase.com/document/viewdoc/?id=449
      

  8.   

    CFileFind   tempFind;
    CString path="c:\\temp\\*.*";
    CString strFileName;
    int nCount=0;

    BOOL bIsFinded =(BOOL)tempFind.FindFile( path );
    while( bIsFinded )
    {
    bIsFinded = (BOOL)tempFind.FindNextFile();
    if( !tempFind.IsDots() ) 
    {
    nCount++;//文件数
    //每次得到一个文件名
    strFileName = tempFind.GetFileName();
    }
    }
    tempFind.Close();
      

  9.   

    copy过去就用!void CTest6Dlg::OnButton2() 
    { CFileFind         tempFind;
    CString path="c:\\temp";
    CString strFileName;
    int          nCount=0;
    BOOL bIsFinded; bIsFinded = (BOOL)tempFind.FindFile( path );
    if(!bIsFinded)
    {
    AfxMessageBox("目录不存在!");
    return;
    }

    path += "\\*.*";
    bIsFinded = (BOOL)tempFind.FindFile( path ); while( bIsFinded )
    {
    bIsFinded = (BOOL)tempFind.FindNextFile();
    if( !tempFind.IsDots() ) 
    {
    nCount++;
    //每次得到一个文件名
    strFileName = tempFind.GetFileName();
    }
    }
    tempFind.Close();
    }