static int GM_CDECL gmfDeleteFolder(gmThread * a_thread) { GM_CHECK_NUM_PARAMS(1); GM_CHECK_STRING_PARAM(path, 0); GM_INT_PARAM(removeSubFolders, 1, 0); if(removeSubFolders) { a_thread->PushInt(RecurseDeletePath(path) ? 1 : 0); } else { a_thread->PushInt(RemoveDirectory(path) ? 1 : 0); } return GM_OK; }
bool RecurseDeletePath(const char * a_path) { WIN32_FIND_DATA findData; char path[MAX_PATH] = ""; strcpy(path,a_path); // remove trailing '\' char int last = (int)strlen(path) - 1; if(path[last] == '\\') { path[last] = '\0'; } // is path valid HANDLE h = FindFirstFile(path,&findData); // path not could not be found OR path is a file, not a folder if((h == INVALID_HANDLE_VALUE) || (!(findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))) { return false; } FindClose(h); h = NULL; // push current working directory char currDir[MAX_PATH + 1] = ""; GetCurrentDirectory(MAX_PATH,currDir); SetCurrentDirectory(path); // iterate over contents of folder h = FindFirstFile("*",&findData); if(h != INVALID_HANDLE_VALUE) { for(;;) { if(strcmp(findData.cFileName,".") != 0 && strcmp(findData.cFileName,"..") != 0) { if(findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { RecurseDeletePath(findData.cFileName); } else { DWORD attrs = GetFileAttributes(findData.cFileName); if(attrs & FILE_ATTRIBUTE_READONLY) { SetFileAttributes(findData.cFileName,attrs ^ FILE_ATTRIBUTE_READONLY); } if(DeleteFile(findData.cFileName) != TRUE) { DWORD res = GetLastError(); printf("\nDeleteFile() returned '%d'..\n",(int)res); } } } if(!FindNextFile(h,&findData)) break; } } // pop current working directory SetCurrentDirectory(currDir); FindClose(h); h = NULL; // remove this directory DWORD attrs = GetFileAttributes(path); if(attrs & FILE_ATTRIBUTE_READONLY) { SetFileAttributes(path,attrs ^ FILE_ATTRIBUTE_READONLY); } return RemoveDirectory(path) != 0; }
bool RecurseDeletePath( std::string path, u8 depth ) { bool ret = true; int i; sysFSStat stat; if( sysFsStat( path.c_str(), &stat ) ) return false; //its a directory if( stat.st_mode & S_IFDIR ) { if( path.at( path.size() - 1 ) != '/' ) path += "/"; s32 fd; sysFSDirent entry; u64 read; //open dir i = sysFsOpendir( path.c_str(), &fd ); if( i ) { printf("sysFsOpendir( %s ): %i\n", path.c_str(), i ); return false; } while( !sysFsReaddir( fd, &entry , &read ) && read > 0 ) { if( !entry.d_namlen || !strcmp( entry.d_name, "." ) || !strcmp( entry.d_name, ".." ) ) { continue; } std::string subPath = path + entry.d_name; if( sysFsStat( subPath.c_str(), &stat ) ) continue; if( stat.st_mode & S_IFDIR ) { if( depth < MAX_RECURSE_DEPTH ) { if( !RecurseDeletePath( subPath, depth ) ) { ret = false; break; } } else { printf("RecurseDeletePath( \"%s\" ): max recursion depth reached\n", subPath.c_str() ); ret = false; break; } } else { i = sysFsUnlink( subPath.c_str() ); if( i ) { printf("sysFsUnlink( %s ): %i\n", subPath.c_str(), i ); ret = false; break; } } } sysFsClosedir( fd ); if( ret ) { i = sysFsRmdir( path.c_str() ); if( i ) { printf("sysFsRmdir( %s ): %08x\n", path.c_str(), i ); ret = false; } } } else//its a file { i = sysFsUnlink( path.c_str() ); if( i ) { printf("sysFsUnlink( %s ): %i\n", path.c_str(), i ); ret = false; } } return ret; }