怎样知道一个文件夹有那些文件

解决方案 »

  1.   

    _findfirst(...,"*.*",...);
    _findnext(...,"*.*",...);
    ...
    这样会找出两个目录出来 "."和"..",你把它们去掉就好了.
      

  2.   

    void IndexFile::CreateFileList( AnsiString& srcpath )
    {
        long findhandle = 0;
        _finddata_t fd;
        HANDLE hf;
        FILETIME ft;
    AnsiString path = srcpath;
        if( path.Length() > 0 && path[path.Length()] != '\\')
         path += '\\';
        AnsiString str = path + "*.*";
        AnsiString perrecode;
        char attbuf[15] = "\x003\x004\x000\x000\x000\x000\x000\x000\x000\x000\x000\x000\x000\x000\n";
        if( m_Stop )
        {
            Form2->Label1->Caption = "正在停止...";
         return;
        }
        findhandle = _findfirst(str.c_str(), &fd);
    if( findhandle != -1 )
    {
         if( ( fd.attrib & _A_SUBDIR ) == _A_SUBDIR )
         {
             if( AnsiString(fd.name) != '.' && AnsiString(fd.name) != "..")
                {
                    m_ItemCount ++;
                 perrecode = path + fd.name;
    fwrite( perrecode.c_str()+m_DstDir.Length(), 1, perrecode.Length()-m_DstDir.Length(), m_IdxFile );
                    fwrite( "\x003\x005\x000\x000\x000\x000\x000\x000\x000\x000\x000\x000\x000\x000\n", 1, 15, m_IdxFile);
                 CreateFileList(perrecode);
                }
            }
         else
         {
                m_ItemCount++;
         perrecode = path + fd.name;
                ft.dwLowDateTime = 0;
                ft.dwHighDateTime = 0;
                hf = CreateFile( perrecode.c_str(), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL );
                if( hf != INVALID_HANDLE_VALUE )
                {
                 GetFileTime( hf, NULL, NULL, &ft );
                    CloseHandle( hf );
                }
                memcpy( attbuf+2, &(fd.size), 4);
                memcpy( attbuf+6, &(ft.dwLowDateTime), 4);
                memcpy( attbuf+10, &(ft.dwHighDateTime), 4);
    fwrite( perrecode.c_str()+m_DstDir.Length(), 1, perrecode.Length()-m_DstDir.Length(), m_IdxFile );
                fwrite( attbuf, 1, 15, m_IdxFile) ;
            }
            int fr = 0;
            MSG msg;
            do
            {
        if( m_Stop )
         {
             Form2->Label1->Caption = "正在停止...";
         return;
         }
             if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
             {
             TranslateMessage(&msg);
             DispatchMessage(&msg);
             }         fr = _findnext(findhandle, &fd);
             if( fr!=0)
                 break;
                if( ( fd.attrib & _A_SUBDIR ) == _A_SUBDIR )
         {
             if( AnsiString(fd.name) != '.' && AnsiString(fd.name) != "..")
                    {
                        m_ItemCount++;
                 perrecode = path + fd.name;
    fwrite( perrecode.c_str()+m_DstDir.Length(), 1, perrecode.Length()-m_DstDir.Length(), m_IdxFile );
                     fwrite( "\x003\x005\x000\x000\x000\x000\x000\x000\x000\x000\x000\x000\x000\x000\n", 1, 15, m_IdxFile);
                 CreateFileList(perrecode);
                    }
             }
         else
         {
                    m_ItemCount++;
         perrecode = path + fd.name;
                    ft.dwLowDateTime = 0;
                 ft.dwHighDateTime = 0;
                 hf = CreateFile( perrecode.c_str(), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL );
                 if( hf != INVALID_HANDLE_VALUE )
                 {
                 GetFileTime( hf, NULL, NULL, &ft );
                     CloseHandle( hf );
                 }
                 memcpy( attbuf+2, &(fd.size), 4);
                 memcpy( attbuf+6, &(ft.dwLowDateTime), 4);
                 memcpy( attbuf+10, &(ft.dwHighDateTime), 4);
    fwrite( perrecode.c_str()+m_DstDir.Length(), 1, perrecode.Length()-m_DstDir.Length(), m_IdxFile );
                 fwrite( attbuf, 1, 15, m_IdxFile) ;
             }
            }while(fr==0);
            _findclose(findhandle);
        }
    }
    _____________
    这是整个函数.BCB下用的.ANSISTRING 是BCB的字符串类.ANSISTRING::C_STR()和CSTRING的LPCTSTR类似.
      

  3.   

    EnumerateFile("e:\\songs\\");void EnumerateFile(CString sPath)
    {
       WIN32_FIND_DATA fd;   CListBox* pListBox = (CListBox*)GetDlgItem(IDC_LIST1);
       HANDLE hFind = ::FindFirstFile(sPath + "*.*",&fd);

       if (hFind != INVALID_HANDLE_VALUE)
       {

          while (::FindNextFile(hFind,&fd))
          {
             //判断是否为目录
             if (fd.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY)
             {
                CString name;
                name = fd.cFileName;
                //判断是否为.和..
                if ((name != ".") && (name != ".."))
                {
                   //如果是真正的目录,进行递归
                   EnumerateFile(sPath + fd.cFileName + "\\");
                }
             }
             pListBox->InsertString(-1,fd.cFileName);

          }

       ::FindClose(hFind);
    }