Thread overview
sending a file to the Recycle Bin or Trashcan
Jan 30, 2008
jicman
Jan 30, 2008
Regan Heath
Jan 30, 2008
Regan Heath
Jan 31, 2008
jicman
January 30, 2008
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?

thanks,

josé
January 30, 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
January 30, 2008
Add these to my last post:

UINT	FO_DELETE         		= 0x0003;
UINT	FOF_ALLOWUNDO     		= 0x0040;
UINT	FOF_NOCONFIRMATION		= 0x0010;

and use this main:

void main()
{
	SHFILEOPSTRUCTA op;
	
    op.wFunc = FO_DELETE;
    op.pFrom = toStringz("C:\\test.txt");
    op.fFlags = FOF_ALLOWUNDO;
    op.fFlags = FOF_ALLOWUNDO + FOF_NOCONFIRMATION;

	writefln(SHFileOperationA(&op));
}

and it puts "C:\\test.txt" in my receycle bin :)

R
January 31, 2008
Regan Heath Wrote:

> Add these to my last post:
> 
> UINT	FO_DELETE         		= 0x0003;
> UINT	FOF_ALLOWUNDO     		= 0x0040;
> UINT	FOF_NOCONFIRMATION		= 0x0010;
> 
> and use this main:
> 
> void main()
> {
> 	SHFILEOPSTRUCTA op;
> 
>      op.wFunc = FO_DELETE;
>      op.pFrom = toStringz("C:\\test.txt");
>      op.fFlags = FOF_ALLOWUNDO;
>      op.fFlags = FOF_ALLOWUNDO + FOF_NOCONFIRMATION;
> 
> 	writefln(SHFileOperationA(&op));
> }
> 
> and it puts "C:\\test.txt" in my receycle bin :)
> 
> R

Thanks Regan.

josé