Cocos2.2.3文件夹的创建与递归删除
发布时间:2020-12-14 16:55:16 所属栏目:百科 来源:网络整理
导读:本文介绍了win32、android下文件夹的创建以及递归删除,下述代码都可直接在Cocos中调用头文件:#if (CC_TARGET_PLATFORM != CC_PLATFORM_WIN32)#include sys/types.h#include sys/stat.h#include errno.h #include stdio.h#include dirent.h#endif代码://An
本文介绍了win32、android下文件夹的创建以及递归删除,下述代码都可直接在Cocos中调用 头文件: #if (CC_TARGET_PLATFORM != CC_PLATFORM_WIN32) #include <sys/types.h> #include <sys/stat.h> #include <errno.h> #include <stdio.h> #include <dirent.h> #endif 代码: //Android or win32 创建文件夹 bool DLManager::createDirectory(const char * path) { #if (CC_TARGET_PLATFORM != CC_PLATFORM_WIN32) mode_t processMask = umask(0); int ret = mkdir(path,S_IRWXU | S_IRWXG | S_IRWXO); umask(processMask); if (ret != 0 && (errno != EEXIST)) { CCLog("mkdir failed..."); return false; } return true; #else BOOL ret = CreateDirectoryA(path,NULL); if (!ret && ERROR_ALREADY_EXISTS != GetLastError()) { return false; } return true; #endif } //Android or win32 递归删除文件夹 bool DLManager::deleteDirectory(const char * path) { #if (CC_TARGET_PLATFORM != CC_PLATFORM_WIN32) DeleteDirectory(path); #else return DeleteDirectoryWin32(path); #endif } //Android 下判断是否为文件夹————引用自http://blog.csdn.net/honty/article/details/7822923 int DLManager::IsDirectory(const char * filename) { #if (CC_TARGET_PLATFORM != CC_PLATFORM_WIN32) struct stat buf; int ret = stat(filename,&buf); if(0 == ret) { if(buf.st_mode & S_IFDIR) { //printf("%s is foldern",filename); return 0; } else { //printf("%s is filen",filename); return 1; } } return -1; #endif return -1; } //Android 下递归删除文件夹————引用自http://blog.csdn.net/honty/article/details/7822923 int DLManager::DeleteDirectory(const char * dirname) { #if (CC_TARGET_PLATFORM != CC_PLATFORM_WIN32) char chBuf[256]; DIR * dir = NULL; struct dirent *ptr; int ret = 0; dir = opendir(dirname); if(NULL == dir) { return -1; } while((ptr = readdir(dir)) != NULL) { ret = strcmp(ptr->d_name,"."); if(0 == ret) { continue; } ret = strcmp(ptr->d_name,".."); if(0 == ret) { continue; } snprintf(chBuf,256,"%s/%s",dirname,ptr->d_name); ret = IsDirectory(chBuf); if(0 == ret) { //printf("%s is dirn",chBuf); ret = DeleteDirectory(chBuf); if(0 != ret) { return -1; } } else if(1 == ret) { //printf("%s is filen",chBuf); ret = remove(chBuf); if(0 != ret) { return -1; } } } (void)closedir(dir); ret = remove(dirname); if(0 != ret) { return -1; } return 0; #endif return -1; } //win32 下判断是否为文件夹————引用自http://blog.csdn.net/jaff20071234/article/details/6559533 bool DLManager::IsDirectoryWin32(const char *pDir) { #if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) char szCurPath[500]; ZeroMemory(szCurPath,500); sprintf_s(szCurPath,500,"%s//*",pDir); WIN32_FIND_DATAA FindFileData; ZeroMemory(&FindFileData,sizeof(WIN32_FIND_DATAA)); HANDLE hFile = FindFirstFileA(szCurPath,&FindFileData); /**< find first file by given path. */ if( hFile == INVALID_HANDLE_VALUE ) { FindClose(hFile); return FALSE; /** 如果不能找到第一个文件,那么没有目录 */ }else { FindClose(hFile); return TRUE; } #endif return false; } //win32 下递归删除文件夹————引用自http://blog.csdn.net/jaff20071234/article/details/6559533 bool DLManager::DeleteDirectoryWin32(const char * DirName) { #if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) //http://blog.csdn.net/jaff20071234/article/details/6559533 // CFileFind tempFind; //声明一个CFileFind类变量,以用来搜索 char szCurPath[MAX_PATH]; //用于定义搜索格式 _snprintf(szCurPath,MAX_PATH,"%s//*.*",DirName); //匹配格式为*.*,即该目录下的所有文件 WIN32_FIND_DATAA FindFileData; ZeroMemory(&FindFileData,sizeof(WIN32_FIND_DATAA)); HANDLE hFile = FindFirstFileA(szCurPath,&FindFileData); BOOL IsFinded = TRUE; while(IsFinded) { IsFinded = FindNextFileA(hFile,&FindFileData); //递归搜索其他的文件 if( strcmp(FindFileData.cFileName,".") && strcmp(FindFileData.cFileName,"..") ) //如果不是"." ".."目录 { string strFileName = ""; strFileName = strFileName + DirName + "//" + FindFileData.cFileName; string strTemp; strTemp = strFileName; if( IsDirectoryWin32(strFileName.c_str()) ) //如果是目录,则递归地调用 { printf("目录为:%s/n",strFileName.c_str()); DeleteDirectoryWin32(strTemp.c_str()); } else { DeleteFileA(strTemp.c_str()); } } } FindClose(hFile); BOOL bRet = RemoveDirectoryA(DirName); if( bRet == 0 ) //删除目录 { printf("删除%s目录失败!/n",DirName); return FALSE; } return TRUE; #endif return false; } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- 小工具Demo第二期---通过正则表达式提取网页中的邮箱
- swift – 与Alamofire的NSURLSession并发请求
- ruby-on-rails – 允许用户在Devise中创建其他用户?
- 《Effective C++》读书笔记 条款52:写了placement new也要
- XML的增、删、查、改
- Oracle 11g安装及遇到的问题
- c – 如何使用顶点的测地距离平滑骨骼顶点权重?
- Oracle SQL:使用REGEXP_SUBSTR表达式的插入查询非常长(拆分
- c# – 使用RestSharp发送HTTP POST Multipart / form-data字
- 四极管:WinCE Bootloader学习笔记之 OEMPlatformInit()函数