sending a file to the Recycle Bin or Trashcan

Regan Heath regan at netmail.co.nz
Wed Jan 30 08:56:44 PST 2008


jicman wrote:
> Greetings!
> 
> Is there a way to send a file to the recycle bin or trashcan in
> Windows?  I know I can delete it using std.file, but how can I send
> it to the recycle bin?

I googled this VB example, you can usually convert VB to C/C++ fairly 
easily:
http://www.cpearson.com/excel/recycle.aspx

Search MSDN for SHFileOperation.  It's declared in shellapi.h and you'll 
need to link your D code with shell32.lib or shell32.dll.

eg

--[recycle.d]--
import std.c.windows.windows;

extern(Windows)
{
	alias WORD FILEOP_FLAGS;

	struct SHFILEOPSTRUCTA
	{
			HWND            hwnd;
			UINT            wFunc;
			LPCSTR          pFrom;
			LPCSTR          pTo;
			FILEOP_FLAGS    fFlags;
			BOOL            fAnyOperationsAborted;
			LPVOID          hNameMappings;
			LPCSTR           lpszProgressTitle; // only used if FOF_SIMPLEPROGRESS
	}
	alias SHFILEOPSTRUCTA* LPSHFILEOPSTRUCTA;
	
	struct SHFILEOPSTRUCTW
	{
			HWND            hwnd;
			UINT            wFunc;
			LPCWSTR         pFrom;
			LPCWSTR         pTo;
			FILEOP_FLAGS    fFlags;
			BOOL            fAnyOperationsAborted;
			LPVOID          hNameMappings;
			LPCWSTR          lpszProgressTitle; // only used if FOF_SIMPLEPROGRESS
	}
	alias SHFILEOPSTRUCTW* LPSHFILEOPSTRUCTW;
	
	int SHFileOperationA(LPSHFILEOPSTRUCTA lpFileOp);
	int SHFileOperationW(LPSHFILEOPSTRUCTW lpFileOp);
}

void main()
{
	SHFILEOPSTRUCTA op;
	// set op members
	SHFileOperationA(&op);
}

To compile:
dmd recycle.d shell32.lib

Regan


More information about the Digitalmars-d-learn mailing list