From 0cd02d1752aaf316af1d360304ed452f3cb633a0 Mon Sep 17 00:00:00 2001 From: luca0N! <63941044+luca0N@users.noreply.github.com> Date: Fri, 19 Jun 2020 12:12:45 -0300 Subject: [PATCH] Added removal routine --- main.c | 40 ++++++++++++++++++++++++++++++++++++++-- str.h | 1 + 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/main.c b/main.c index e990380..ab78c17 100644 --- a/main.c +++ b/main.c @@ -27,6 +27,30 @@ #include #include +// 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)); diff --git a/str.h b/str.h index c89b157..e9b4154 100644 --- a/str.h +++ b/str.h @@ -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 para mais informações."