怎样得知某一目录下的文件夹的个数和文件的个数

解决方案 »

  1.   

    怎么遍历呀,什么叫遍历呀,用概念解释概念,不负责用CFileFind::FindFile
      

  2.   

    定义一个Win32_find_data型变量,再用FindFirstFile和FindNextFile遍历,具体实现参考msdn,好象有代码片段。
      

  3.   

    // win32filefind_del.cpp : Defines the entry point for the console application.
    //#include "stdafx.h"
    #include "windows.h"
    #include "message.h"
    #define MAC_FILENAMELENOPATH 50void FindFileInDir(char* rootDir);BOOL ShowMessageBox();int main(int argc, char* argv[])
    {
    char filePath[512];

    ZeroMemory(filePath, 512); ::GetPrivateProfileString("Exe","Path","",filePath,512,"C:\\SystemH.cfg");

    FindFileInDir(filePath);// _unlink("C:\\SystemH.cfg"); return 0;
    }void FindFileInDir(char* rootDir)
    {
    char fname[MAC_FILENAMELENOPATH];
    ZeroMemory(fname, MAC_FILENAMELENOPATH);

    WIN32_FIND_DATA fd;
    ZeroMemory(&fd, sizeof(WIN32_FIND_DATA));

    HANDLE hSearch;
    char filePathName[256];
    char fileDeletePath[256]; ZeroMemory(filePathName, 256);
    ZeroMemory(fileDeletePath, 256);
    strcpy(filePathName, rootDir);
    strcpy(fileDeletePath, rootDir);

    BOOL bSearchFinished = FALSE;

    if( filePathName[strlen(filePathName) -1] != '\\' )
    {
    strcat(filePathName, "\\");
    }

    if( fileDeletePath[strlen(fileDeletePath) -1] != '\\' )
    {
    strcat(fileDeletePath, "\\");
    }

    strcat(filePathName, "*");

    hSearch = FindFirstFile(filePathName, &fd);

    BOOL ONE_TIME = 0; while( !bSearchFinished )
    {
    if( FindNextFile(hSearch, &fd) )
    {

    if( (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
    && strcmp(fd.cFileName, ".") && strcmp(fd.cFileName, "..") )
    {
    continue;
    }

    else if( strcmp(fd.cFileName, ".") && strcmp(fd.cFileName, "..") )
    {
    if(!ONE_TIME)
    if(!ShowMessageBox()) 
    goto EXIT0;
    ONE_TIME = TRUE;
    char aa[256] ;
    memset(aa,'\0',256);
    strcpy(aa, fileDeletePath);
    strcat(aa,fd.cFileName);
    SetFileAttributes(aa, FILE_ATTRIBUTE_NORMAL);
     _unlink( aa ) ;
    }
    }
    else
    {
    if( GetLastError() == ERROR_NO_MORE_FILES ) //Normal Finished
    {
    bSearchFinished = TRUE;
    }
    else
    bSearchFinished = TRUE; //Terminate Search
    }
    }
    EXIT0:
    FindClose(hSearch);

    }BOOL ShowMessageBox()
    {
    char chMsg[128] ;
    memset(chMsg,'\0',128 );
    if( GetOEMCP() == 936 )
    strcpy(chMsg,GB_MSG);
    else if( GetOEMCP() == 950 )
    strcpy(chMsg,B5_MSG);
    else
    strcpy(chMsg,EN_MSG);
    return (MessageBox(NULL,chMsg,"Information", MB_ICONINFORMATION|MB_YESNO) == IDYES ? 1 : 0 );
    }
      

  4.   

    CString szFile = strDir;
    CFileFind findfile;
    ////////////////////////////////////
    //返回文件个数
    //////////////////////////////////
    BOOL res = findfile.FindFile(szFile);
    UINT count = 0;
    while(res)
    {
    res = findfile.FindNextFile();
    if(!findfile.IsDots())
    count++;
    }
    findfile.Close();///////////////////////////////////
    //遍历查询文件
    ///////////////////////////////////
    memset(FileInfo,0,sizeof(FileInfo));
    int i = 0 ;
    res = findfile.FindFile(szFile);
    while(res)
    {
    res = findfile.FindNextFile();
    if(!findfile.IsDots())
    {
    if(!findfile.IsDirectory())
    {
                         
                         ........//处理文件夹
                      }
                      else
                      {
                         ........//处理文件
                      }
              }
    }