Programming Tips - win32: programmatically move a file to the Windows recycle bin

Date: 2007dec29 Update: 2025jan13 Platform: win32 Language: C/C++ Q. win32: programmatically move a file to the Windows recycle bin A. Here is a function that does just that. (This is nice thing to do if a user of your program ever requests that a file be deleted.)
// Delete by moving to reycle bin BOOL RecycleFile(LPCSTR szFileIn) { char szFile[MAX_PATH + 2]; SHFILEOPSTRUCT op; lstrcpyn(szFile, szFileIn, sizeof(szFile)); szFile[lstrlen(szFile) + 1] = '\0'; // Extra terminating NUL required ZeroMemory(&op, sizeof(op)); op.wFunc = FO_DELETE; op.pFrom = szFile; op.fFlags = FOF_ALLOWUNDO | FOF_NOCONFIRMATION | FOF_NOERRORUI | FOF_SILENT; // Options set for no user interaction return 0 == SHFileOperation(&op); }