how can I remove a directory and its' files and its' sub-dirs?

how can I remove a directory and its’ files and its’ sub-dirs in cocos2d-x?

Thanks a lot!

@
BOOL XAssetsManager::RemoveDirectory(const char szPath[])
{
#if (CC_TARGET_PLATFORM != CC_PLATFORM_WIN32)
BOOL bResult = false;
int nRetCode = 0;
DIR* pDir = opendir(szPath);
struct dirent* pDirent = NULL;
struct stat statBuf;
char szSubPath[MAX_PATH];

XYLOG_FAILED_JUMP(pDir);

while (true)
{
pDirent = readdir(pDir);
if (!pDirent)
break;

if (!strcmp(pDirent~~>d_name, “.”) || !strcmp)
continue;
nRetCode = snprintf, “s/s”, szPath, pDirent~~>d_name);
XYLOG_FAILED_JUMP(nRetCode > –1 && nRetCode < _countof(szSubPath));

if (stat(szSubPath, &statBuf))
continue;

if (S_ISDIR(statBuf.st_mode))
{
nRetCode = RemoveDirectory(szSubPath);
XYLOG_FAILED_JUMP(!nRetCode);
}
else
{
nRetCode = unlink(szSubPath);
XYLOG_FAILED_JUMP(!nRetCode);
}
}

bResult = true;
Exit0:
if (pDir)
{
closedir(pDir);
}

if (bResult)
{
nRetCode = rmdir(szPath);
bResult = nRetCode != 0;
}
return bResult;
#else
return false;
#endif
}
@

Thanks.
Can it work on iOS? It looks that it works in Windows.

Dpull com wrote:

@
BOOL XAssetsManager::RemoveDirectory(const char szPath[])
{
#if (CC_TARGET_PLATFORM != CC_PLATFORM_WIN32)
BOOL bResult = false;
int nRetCode = 0;
DIR* pDir = opendir(szPath);
struct dirent* pDirent = NULL;
struct stat statBuf;
char szSubPath[MAX_PATH];
>
XYLOG_FAILED_JUMP(pDir);
>
while (true)
{
pDirent = readdir(pDir);
if (!pDirent)
break;
>
if (!strcmp(pDirent~~>d_name, “.”) || !strcmp)
continue;
>
nRetCode = snprintf, “s/s”, szPath, pDirent~~>d_name);
XYLOG_FAILED_JUMP(nRetCode > –1 && nRetCode < _countof(szSubPath));
>
if (stat(szSubPath, &statBuf))
continue;
>
if (S_ISDIR(statBuf.st_mode))
{
nRetCode = RemoveDirectory(szSubPath);
XYLOG_FAILED_JUMP(!nRetCode);
}
else
{
nRetCode = unlink(szSubPath);
XYLOG_FAILED_JUMP(!nRetCode);
}
}
>
bResult = true;
Exit0:
if (pDir)
{
closedir(pDir);
}
>
if (bResult)
{
nRetCode = rmdir(szPath);
bResult = nRetCode != 0;
}
return bResult;
#else
return false;
#endif
}
@

I added this method in CCFileUtils to remove a folder and its’ files on iOS:

bool CCFileUtils::removeDirAndFiles(const char pDirectoryName)
{
NSArray
paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString documentsDirectory = ;
NSString
delPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithUTF8String:pDirectoryName]];

if ([[NSFileManager defaultManager] fileExistsAtPath:delPath])
{
NSFileManager *fileManager = [NSFileManager defaultManager];
[fileManager removeItemAtPath:delPath error:nil];

return true;
}

return false;

}

I added this method in CCFileUtils to know if a dir or file is exist on iOS:

bool CCFileUtils::isFileExist(const char pName)
{
NSArray
paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString documentsDirectory = ;
NSString
newPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithUTF8String:pName]];

if ([[NSFileManager defaultManager] fileExistsAtPath:newPath])
{
return true;
}
else
return false;
}