Added removal routine

This commit is contained in:
luca0N! 2020-06-19 12:12:45 -03:00
parent 8805985855
commit 0cd02d1752
No known key found for this signature in database
GPG Key ID: 9F1B99B522287CAB
2 changed files with 39 additions and 2 deletions

40
main.c
View File

@ -27,6 +27,30 @@
#include <stdio.h>
#include <string.h>
// Removes a directory using the Windows shell
int shellRemoveDir(LPCTSTR dirPath){
// Remove the directory
//printf("!! ATTEMPT TO REMOVE %s\\...\n", dirPath);
// Append null to the end of the path
// Double null terminated path string
char dntps[strlen(dirPath) + 2];
strcpy(dntps, dirPath);
strcat(dntps, "\\\0");
SHFILEOPSTRUCT dir = {
NULL,
FO_DELETE,
dntps,
NULL,
FOF_NOCONFIRMATION | FOF_NOERRORUI,
0,
0,
""
};
return SHFileOperation(&dir);
}
// Prints the program license to the user.
int printLicense(int full){
if (full == 0) {
@ -83,17 +107,29 @@ void cleanDir(const char* path){
if (iteration <= 2)
continue;
printf("%s %s\n", STR_FOUND_ITEM, data.cFileName);
// Attempt to delete file...
// Attempt to delete item...
int pathSize = strlen(wildPath) + strlen(data.cFileName);
char fullPath[pathSize];
strcpy(fullPath, path);
strcat(fullPath, data.cFileName);
// If the item is a directory, then delete it through the shell
if (data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY){
int shellReturnCode = shellRemoveDir(fullPath);
if (shellReturnCode != 0){
printf(" %s\n", STR_FAILED_DIR);
printf(" --> %s\n", fullPath);
printf(" --> %#010x\n", shellReturnCode);
}
continue;
}
if (DeleteFile(fullPath) == 0){
int lastError = GetLastError();
printf(" %s\n", STR_FAILED_FILE);
printf(" --> %s\n", fullPath);
printf(" --> %#010x\n", GetLastError());
printf(" --> %#010x\n", lastError);
} else
printf(" %s\n", STR_DELETED_FILE);
} while(FindNextFile(hFind, &data));

1
str.h
View File

@ -35,6 +35,7 @@
#define STR_FOUND_ITEM "[Item encontrado]"
#define STR_FAILED_DIR_LIST "Um erro ocorreu ao tentar enumerar os arquivos dentro de uma pasta especificada."
#define STR_FAILED_FILE "[Arquivo mantido] Não foi possível remover este arquivo pelo seguinte erro:"
#define STR_FAILED_DIR "[Pasta mantida] Não foi possível remover esta pasta pelo seguinte erro:"
#define STR_ERROR_LIC "Falha ao ler arquivo de licença. Visite <https://www.gnu.org/licenses/> para mais informações."