// Delete files to the Recycle Bin #include #include #include static char HELP[] = "\n" "RDEL file file file ...\n" "\n" "Deletes the specified files, sending them to the Recycle Bin.\n" "Ambiguous filespecs may be used to delete multiple files.\n" "Hidden and system files will not be deleted"; bool RecycleFile(const char *specs) { SHFILEOPSTRUCT info = {NULL}; char complete_specs[MAX_PATH+2] = {'\0'}; char *dummy; if (GetFullPathName(specs, sizeof(complete_specs), complete_specs, &dummy) != 0) { info.wFunc = FO_DELETE; info.pFrom = complete_specs; info.fFlags = FOF_ALLOWUNDO | FOF_NOCONFIRMATION| FOF_FILESONLY; return SHFileOperation(&info) == 0; } return false; } const DWORD EXCLUDED_FILES = FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_SYSTEM; void main(int argc, char **argv) { if (argc < 2) puts(HELP); else { for (int i = 1; i < argc; i++) { char drive[MAXDRIVE]; char directory[MAXDIR]; // char name[MAXFILE]; // char extension[MAXEXT]; fnsplit(argv[i], drive, directory, NULL, NULL); WIN32_FIND_DATA data; HANDLE handle = FindFirstFile(argv[i], &data); if (handle != INVALID_HANDLE_VALUE) { do { if ((data.dwFileAttributes & EXCLUDED_FILES) == 0) { char specs[MAXPATH+1]; strcpy(specs, drive); strcat(specs, directory); strcat(specs, data.cFileName); printf("Deleting %s\n", specs); RecycleFile(specs); } } while (FindNextFile(handle, &data)); FindClose(handle); } } } }