需求分析:
删除所给目录下所有子目录和文件
方法1:
递归删除目录下所有文件,将目录置成空目录,删除空目录
方法2:
POCO库(非递归方法)
方法1实现:
int deleteDir(const char* dirPath)
{
<span style="white-space:pre"> </span>vector<string> vectPathLst;
<span style="white-space:pre"> </span>char path[250];
//制作路径
<span style="white-space:pre"> </span>strcpy(path, dirPath);
<span style="white-space:pre"> </span>findFileInDir(path, "*", vectPathLst);
<span style="white-space:pre"> </span>vector<string>::iterator iter = vectPathLst.end() - 1;
<span style="white-space:pre"> </span>string strPath = *iter;
<span style="white-space:pre"> </span>Poco::Path TmpPath = strPath;
<span style="white-space:pre"> </span>do{
<span style="white-space:pre"> </span>if (vectPathLst.size() <= 0){
<span style="white-space:pre"> </span>break;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>if (iter == vectPathLst.end()){
<span style="white-space:pre"> </span>break;
<span style="white-space:pre"> </span>}
//是目录,继续搜索下一级目录,
<span style="white-space:pre"> </span>if (TmpPath.isDirectory()){
<span style="white-space:pre"> </span>deleteDir(strPath.c_str());
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>else{//是文件,删除文件
<span style="white-space:pre"> </span>remove(strPath.c_str());
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>vectPathLst.erase(iter);
<span style="white-space:pre"> </span>} while (true);
//删除子目录下所有文件后,删除目录
<span style="white-space:pre"> </span>remove(path);//删除空目录的方法 需修改
<span style="white-space:pre"> </span>return 0;
}
bool findFileInDir(const char* pchInDir, const char* pchFilter, vector<string> & vecOutFileLst, bool bReturnPath)
{
<span style="white-space:pre"> </span>bool bFind = false;
<span style="white-space:pre"> </span>string strPath;
<span style="white-space:pre"> </span>strPath = pchInDir;
<span style="white-space:pre"> </span>try {
<span style="white-space:pre"> </span>Poco::DirectoryIterator it( strPath );
<span style="white-space:pre"> </span>Poco::DirectoryIterator end;
<span style="white-space:pre"> </span>if ( it == end )
<span style="white-space:pre"> </span>return bFind;
<span style="white-space:pre"> </span>if ( bReturnPath ) {
<span style="white-space:pre"> </span>strPath = pchInDir;
<span style="white-space:pre"> </span>if ( strPath[ strPath.length() - 1 ] != '/' )
<span style="white-space:pre"> </span>strPath += "/";
<span style="white-space:pre"> </span>} else
<span style="white-space:pre"> </span>strPath = "";
<span style="white-space:pre"> </span>string strOutFile;
//定义筛选条件
<span style="white-space:pre"> </span>vector<string> vectStr;
<span style="white-space:pre"> </span>if ( pchFilter[ 0 ] == '*' )
<span style="white-space:pre"> </span>vectStr.push_back( "*" );
<span style="white-space:pre"> </span>GetVectStrFromStrWholeByChar( string( pchFilter ), '*', vectStr );
<span style="white-space:pre"> </span>if ( pchFilter[ strlen( pchFilter ) - 1 ] == '*' )
<span style="white-space:pre"> </span>vectStr.push_back( "*" );
<span style="white-space:pre"> </span>while ( it != end ) {
<span style="white-space:pre"> </span>if ( filterFile( vectStr, it.name().c_str() ) ) {
<span style="white-space:pre"> </span>if ( bReturnPath ) {
<span style="white-space:pre"> </span>strOutFile = strPath + it.name();
<span style="white-space:pre"> </span>} else
<span style="white-space:pre"> </span>strOutFile = it.name();
<span style="white-space:pre"> </span>vecOutFileLst.push_back( strOutFile );
<span style="white-space:pre"> </span>bFind = true;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>it++;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>} catch ( Poco::Exception& ex ) {
<span style="white-space:pre"> </span>logprintf( "Warn: findFileInDir( %s ) %s\n", strPath.c_str(), ex.message().c_str() );
<span style="white-space:pre"> </span>} catch ( ... ) {
<span style="white-space:pre"> </span>logprintf( "Warn: findFileInDir( %s )\n", strPath.c_str() );
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>return bFind;
}
// 1,2,3 ==> 1 2 3, NOTICE: Maybe need trimString first
size_t GetVectStrFromStrWholeByChar(const string& strWhole, const char chSep, vector<string>& vectStr, bool bClean = true)
{
<span style="white-space:pre"> </span>if (bClean)
<span style="white-space:pre"> </span>vectStr.clear() ;
<span style="white-space:pre"> </span>if(strWhole.length() == 0) return 0 ;
<span style="white-space:pre"> </span>string::size_type begPos=0, endPos ; // for string search
<span style="white-space:pre"> </span>while( (endPos=strWhole.find(chSep, begPos)) != string::npos) {
<span style="white-space:pre"> </span>string strSub = strWhole.substr(begPos, endPos-begPos) ;
<span style="white-space:pre"> </span>vectStr.push_back(strSub) ;
<span style="white-space:pre"> </span>begPos = endPos+1 ;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>vectStr.push_back(strWhole.substr(begPos)) ;
<span style="white-space:pre"> </span>return vectStr.size() ;
}
方法2实现:
POCO库
Poco::File sFile( "D:\\AFCTFS" );// D:\\AFCTFS为需要删除目录
sFile.remove( true ); //删除子目录和所有文件
参考:http://blog.csdn.net/blueln/article/details/8700299#userconsent#