Dave's Brain

Browse - programming tips - move file to recycle bin

Date: 2007dec29
Platform: win32
Language: C

Q.  How do I programmatically move a file to the 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 req'd

	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);
}

Add a comment

Sign in to add a comment
Copyright © 2008, dave - Code on Dave's Brain is licensed under the Creative Commons Attribution 2.5 License.