2
* Copyright (c) 2007, 2008 Agostino Russo
4
* Written by Agostino Russo <agostino.russo@gmail.com>
5
* Following example at http://www.codeguru.com/forum/showthread.php?t=239271
7
* pylauncher is free software; you can redistribute it and/or modify
8
* it under the terms of the GNU General Public License as
9
* published by the Free Software Foundation; either version 2 of
10
* the License, or (at your option) any later version.
12
* pylauncher is distributed in the hope that it will be useful,
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
* GNU General Public License for more details.
17
* You should have received a copy of the GNU General Public License
18
* along with this program. If not, see <http://www.gnu.org/licenses/>.
21
* Recursively deletes a directory
24
#include "deletedir.h"
31
int _delete_directory(char* root_directory);
33
int delete_directory(char* target_directory)
35
char abs_path[MAX_PATH];
37
if (GetFullPathName(target_directory, MAX_PATH, abs_path, NULL)!=0){
39
result = _delete_directory(abs_path);
45
int _delete_directory(char* root_directory)
51
WIN32_FIND_DATA file_info;
53
pattern = concat(root_directory, "\\*.*", false);
54
if(!pattern) return ERROR_NOT_ENOUGH_MEMORY;
55
file_handle = FindFirstFile(pattern, &file_info);
57
if(file_handle != INVALID_HANDLE_VALUE){
59
if(file_info.cFileName[0] != '.'){
60
file_path = concat(root_directory, "\\", false);
61
file_path = concat(file_path, file_info.cFileName, true);
63
FindClose(file_handle);
65
return ERROR_NOT_ENOUGH_MEMORY;
68
if(file_info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY){
69
// Delete subdirectory
70
result = _delete_directory(file_path);
72
// Set file attributes
73
if(SetFileAttributes(file_path, FILE_ATTRIBUTE_NORMAL) == FALSE){
74
result = GetLastError();
77
if(DeleteFile(file_path) == FALSE){
78
result = GetLastError();
83
FindClose(file_handle);
88
} while(FindNextFile(file_handle, &file_info) == TRUE);
90
FindClose(file_handle);
93
DWORD dwError = GetLastError();
94
if(dwError != ERROR_NO_MORE_FILES){
97
// Set directory attributes
98
if(SetFileAttributes(root_directory, FILE_ATTRIBUTE_NORMAL) == FALSE){
99
return GetLastError();
102
if(RemoveDirectory(root_directory) == FALSE){
103
return GetLastError();