c++刪除指定文件夾下的所有文件

void DeleteDirectory(CString strPath)
{
    CFileFind tempFind;

    TCHAR sTempFileFind[MAX_PATH] = { 0 };

    wsprintf(sTempFileFind, _T("%s\\*.*"), strPath);

    BOOL IsFinded = tempFind.FindFile(sTempFileFind);

    while (IsFinded)

    {

        IsFinded = tempFind.FindNextFile();

        if (!tempFind.IsDots())

        {

            TCHAR sFoundFileName[200] = { 0 };

            _tcscpy(sFoundFileName, tempFind.GetFileName().GetBuffer(200));

            if (tempFind.IsDirectory())

            {

                TCHAR sTempDir[200] = { 0 };

                wsprintf(sTempDir, _T("%s\\%s"), strPath, sFoundFileName);

                DeleteDirectory(sTempDir); //刪除文件夾下的文件

                RemoveDirectory(sTempDir); //移除空文件

            }

            else

            {

                TCHAR sTempFileName[200] = { 0 };

                wsprintf(sTempFileName, _T("%s\\%s"), strPath, sFoundFileName);

                DeleteFile(sTempFileName);

            }

        }

    }

    tempFind.Close();

}
int main()
{
       CString m_path;
    m_path = "C:\\Users\\YangJingLong\\Desktop\\staistic\\staistic\\data\\";
    DeleteDirectory(m_path);    
    
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章