I made a simple program that deletes files, however, I don't know how to delete a directory. I saw a few posts saying I need to list all of the files in that directory, delete those files, and then delete the directory/folder itself. However, somebody suggested using _rmdir which as far as I understand, deletes a directory without any problems, however, it doesn't. Do I still need to list all of the files in a directory in order to delete it with _rmdir? Thanks!
Code:
#include <iostream> #include <string> #include <sys/stat.h> #include <Windows.h> #include <direct.h> using namespace std; HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE); inline bool fileExists(const string& filepath) { struct stat buffer; return (stat(filepath.c_str(), &buffer) == 0); } int main() { string filePath; string fileAttribute; cout << "[~] Enter a path to delete: "; getline(cin, filePath); cout << "\n[#] Checking if path exists.."; if (fileExists(filePath) == 1) { if (GetFileAttributes(filePath.c_str()) == FILE_ATTRIBUTE_DIRECTORY) { cout << "\n[!] Directory found!"; _rmdir(filePath.c_str()); cout << "\n[#] Deleting directory.."; } else { cout << "\n[!] File found!"; remove(filePath.c_str()); cout << "\n[#] Deleting file.."; } if (fileExists(filePath) == 0) { SetConsoleTextAttribute(h, 10); cout << "\n[!] Deletetion successful!"; SetConsoleTextAttribute(h, 15); } else { SetConsoleTextAttribute(h, 12); cout << "\n[!] Deletion unsuccessful!"; SetConsoleTextAttribute(h, 15); } } }