Send to Recycle Bin

From NSIS Wiki
Jump to navigationJump to search
Author: Anders (talk, contrib)


Description

Silently send file(s) or a directory to the Windows Recycle bin instead of permanently deleting it. Standard MS-DOS wildcard characters (*?) are permitted only in the file-name position. The function return value is pushed on the stack. A non-zero value indicates an error.

Note: This code relies on the undocumented fact that the system plugin zero fills &X struct types


Usage

Section "Example"
WriteINIStr "$TEMP\testfile.ext" foo bar baz   ;We need something to delete
!insertmacro SHFileOperation_Recycle "$temp\testf*e.ex?" $0
DetailPrint RecycleError=$0 ;0 = OK
SectionEnd


The Code

!ifndef FO_DELETE
!define FO_DELETE 0x3
!endif
!ifndef FOF_SILENT
!define FOF_SILENT 0x4
!endif
!ifndef FOF_NOCONFIRMATION
!define FOF_NOCONFIRMATION 0x10
!endif
!ifndef FOF_ALLOWUNDO
!define FOF_ALLOWUNDO 0x40
!endif
!ifndef FOF_NOERRORUI
!define FOF_NOERRORUI 0x400
!endif
 
Function SHFileOperation_Recycle
Exch $0
Push $1
System::Call '*(i$hwndparent,i${FO_DELETE},i,i,i${FOF_ALLOWUNDO}|${FOF_NOCONFIRMATION}|${FOF_SILENT}|${FOF_NOERRORUI},i0,i0,i0,&t${NSIS_MAX_STRLEN} r0,&t1 0)i.r0'
IntOp $1 $0 + 32
System::Call '*$0(i,i,ir1,i,i,i,i,i)'
Pop $1
System::Call 'shell32::SHFileOperation(ir0)i.s'
System::Free $0
Exch
Pop $0
FunctionEnd
!macro SHFileOperation_Recycle filespec outvarwinerr
push "${filespec}"
call SHFileOperation_Recycle
!if "${outvarwinerr}" != "s"
	pop ${outvarwinerr}
!endif
!macroend