技术支持   Support
联系我们   Contact
你的位置:首页 > 技术支持

C++Builder 文件搜寻与列表

2015/2/25 4:53:12      点击:

void __fastcall CrnFindFile(String strDir, String strFileExt, TStrings *pList)

{

    WIN32_FIND_DATA wfd;

    String strFileName;

    if (strDir.LastDelimiter("\\") != strDir.Length())

        strDir += "\\";

    HANDLE hFind = ::FindFirstFile((strDir + "*.*").c_str(), &wfd);

    if (INVALID_HANDLE_VALUE != hFind)

    {

        do

        {

            strFileName = String(wfd.cFileName);

            if (strFileName == "." || strFileName == "..")

                continue;

            // 如果是子目录就进入子目录搜索

            if (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)

                CrnFindFile(strDir + strFileName + "\\", strFileExt, pList);

            // 将指定扩展名的文件名加入到列表中

            if (ExtractFileExt(strFileName).UpperCase() == strFileExt)

                pList->Add(strDir + strFileName);

        } while(::FindNextFile(hFind, &wfd));

    }

    ::FindClose(hFind);

}

void __fastcall TForm1::Button1Click(TObject *Sender)

{

    CrnFindFile("D:\\ccrun\\", ".MP4", Memo1->Lines);

}