ディレクトリの一括消去 WindowsAPIのRemoveDirectoryは中身が入っていると失敗するようにできています。ここでは、中身を含め指定したディレクトリを一括消去する関数を作成してみます。
この関数の引数fileNameは\で終わってはいけません。
bool removeDirectory(string fileName)
{
bool retVal =true;
string nextFileName;
WIN32_FIND_DATA foundFile;
HANDLE hFile = FindFirstFile((fileName + "\\*.*").c_str(),&foundFile);
if(hFile != INVALID_HANDLE_VALUE)
{
do
{
//If a found file is . or .. then skip
if(strcmp(foundFile.cFileName,".")!=0 && strcmp(foundFile.cFileName,"..")!=0)
{
//The path should be absolute path
nextFileName = fileName + "\\" + foundFile.cFileName;
//If the file is a directory
if((foundFile.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)!=0)
{
removeDirectory(nextFileName.c_str());
RemoveDirectory(nextFileName.c_str());
}
//If the file is a file
else
{
DeleteFile(nextFileName.c_str());
}
}
}
while(FindNextFile(hFile,&foundFile)!=0);
}
FindClose(hFile);
//Delete starting point itseft
if(RemoveDirectory(fileName.c_str())==0)retVal=false;
return retVal;
}
目次に戻る
| Copyright(c) 2008 WoodenSoldier Software |
|