Added object count support

The program now displays the amount of deleted objects after the main on exit. In order to know the amount of successfully deleted objects, the shell function (shellRemoveDir) is no longer used.
This commit is contained in:
luca0N! 2020-06-27 01:52:55 -03:00
parent f5b564fc0f
commit c1f6615946
No known key found for this signature in database
GPG Key ID: 9F1B99B522287CAB
2 changed files with 22 additions and 14 deletions

30
main.c
View File

@ -77,14 +77,15 @@ int printLicense(int full){
}
// Cleans a directory.
void cleanDir(const char* path){
void cleanDir(const char* path, unsigned* objectCount){
const char* validPath = path;
//char validPath[sizeof(path) + 1];
//strcpy(validPath, path);
/*// Check if the path ends with '\'
if (path[strlen(path)] != '\\'){
// Check if the path ends with '\'
// Because the last character of a string is null, we should check the character before that one.
if (path[strlen(path) - 1] != '\\'){
strcat(validPath, "\\");
}*/
}
// Path with wildcard
char wildPath[strlen(validPath) + 1];
@ -110,14 +111,15 @@ void cleanDir(const char* path){
strcpy(fullPath, validPath);
strcat(fullPath, data.cFileName);
// If the item is a directory, then delete it through the shell
// If the item is a directory, call cleanDir on that directory.
if (data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY){
int shellReturnCode = shellRemoveDir(fullPath);
cleanDir(fullPath, objectCount);
/*int shellReturnCode = shellRemoveDir(fullPath);
if (shellReturnCode != 0){
printf(" %s\n", STR_FAILED_DIR);
printf(" --> %s\n", fullPath);
printf(" --> %#010x\n", shellReturnCode);
}
}*/
continue;
}
@ -126,8 +128,11 @@ void cleanDir(const char* path){
printf(" %s\n", STR_FAILED_FILE);
printf(" --> %s\n", fullPath);
printf(" --> %#010x\n", lastError);
} else
} else {
printf(" %s\n", STR_DELETED_FILE);
// Update the deleted object count if the process was successful.
(*objectCount)++;
}
} while(FindNextFile(hFind, &data));
} else {
printf("%s\n", STR_FAILED_DIR_LIST);
@ -136,7 +141,7 @@ void cleanDir(const char* path){
// Gets the user defined directories
void getAdditionalDirs(bool safeMode){
unsigned objectCount = 0;
unsigned* objectCount = 0;
printf("\n%s\n", STR_FETCHING_ADD_DIRS);
int dirs;
dirs = GetPrivateProfileInt("Pastas", "Pastas", 3, PATH_CFG);
@ -145,7 +150,7 @@ void getAdditionalDirs(bool safeMode){
for(unsigned short x = 0; x < dirs; x++){
char dirKey[7] = "Pasta";
char* intPtr = (char*) (x + 1) + '0';
strcat(dirKey, &intPtr);
strcat(dirKey, (char*) &intPtr);
_TCHAR currentPath[128];
GetPrivateProfileString("Pastas", dirKey, "null", currentPath,
sizeof(currentPath) / sizeof(currentPath[0]), PATH_CFG);
@ -215,7 +220,7 @@ void getAdditionalDirs(bool safeMode){
printf("%s [%s] %s\n", STR_FOUND_DIR, dirKey, buffer);
if (!safeMode)
cleanDir(buffer);
cleanDir(buffer, &objectCount);
free(buffer);
free(varBuffer);
continue;
@ -224,8 +229,9 @@ void getAdditionalDirs(bool safeMode){
printf("%s [%s] %s\n", STR_FOUND_DIR, dirKey, currentPath);
if (!safeMode)
cleanDir(currentPath);
cleanDir(currentPath, &objectCount);
}
printf(FMT_STR_OBJECT_COUNT, objectCount);
}
int main(void){

6
str.h
View File

@ -34,8 +34,10 @@
#define STR_FOUND_FILE "[Arquivo encontrado]"
#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_FAILED_FILE "[Arquivo mantido] Não foi possível apagar este arquivo pelo seguinte erro:"
#define STR_FAILED_DIR "[Pasta mantida] Não foi possível apagar esta pasta pelo seguinte erro:"
#define FMT_STR_OBJECT_COUNT "[Relatório] %u objetos foram apagados com sucesso.\n"
#define STR_MEM_ERR "[Erro fatal] Não foi possível alocar memória. Tente fechar alguns programas abertos e tente novamente."