Send to Recycle Bin: Difference between revisions

From NSIS Wiki
Jump to navigationJump to search
m (Reverted edits by 60.251.54.208 to last version by ConversionBot)
No edit summary
 
(One intermediate revision by one other user not shown)
Line 1: Line 1:
{{PageAuthor|Iceman_K}}
{{PageAuthor|Anders}}


== Description ==
== Description ==
Silently send a file/files/directory to the Windows Recycle bin instead of permanantly deleting it.
Silently send file(s) or a directory to the Windows Recycle bin instead of permanently deleting it.
This function supports standard windows wildcards.
Standard MS-DOS wildcard characters (<code>*?</code>) are permitted ''only'' in the file-name position.
The return value is pushed on the stack. A non-zero value indicates an error.
The function return value is pushed on the stack. A non-zero value indicates an error.
 
<div style="background-color:#FFFFCC;color:black;border:0.1em solid #ccccaa;padding:0.3em;">Note: This code relies on the undocumented fact that the system plugin zero fills &X struct types</div>
 


== Usage ==
== Usage ==
<highlight-nsis>
<highlight-nsis>
Push "c:\temp\xyz.txt" ; File to delete, supports wildcards
Section "Example"
Call SendToRecycleBin
WriteINIStr "$TEMP\testfile.ext" foo bar baz  ;We need something to delete
Pop $R0 ; Return code
!insertmacro SHFileOperation_Recycle "$temp\testf*e.ex?" $0
DetailPrint RecycleError=$0 ;0 = OK
SectionEnd
</highlight-nsis>
</highlight-nsis>


== The Function ==
Include the following code in your nsi file:


== The Code ==
<highlight-nsis>
<highlight-nsis>
!ifndef FO_DELETE
!ifndef FO_DELETE
Line 28: Line 32:
!ifndef FOF_ALLOWUNDO
!ifndef FOF_ALLOWUNDO
!define FOF_ALLOWUNDO 0x40
!define FOF_ALLOWUNDO 0x40
!endif
!ifndef FOF_NOERRORUI
!define FOF_NOERRORUI 0x400
!endif
!endif


Function SendToRecycleBin
Function SHFileOperation_Recycle
Exch $R0
Exch $0
Push $R1
Push $1
Push $R2
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::Alloc 28
System::Call '*$0(i,i,ir1,i,i,i,i,i)'
Pop $R1
Pop $1
System::Call "*$R1(i $HWNDPARENT, i ${FO_DELETE}, t '$R0', t '', i ${FOF_ALLOWUNDO}|${FOF_SILENT}|${FOF_NOCONFIRMATION}, i 0, i 0, t '')"
System::Call 'shell32::SHFileOperation(ir0)i.s'
System::Call "shell32::SHFileOperationA(i R1)i.R2"
System::Free $0
System::Free $R1
Exch
StrCpy $R0 $R2
Pop $0
Pop $R2
Pop $R1
Exch $R0
FunctionEnd
FunctionEnd
!macro SHFileOperation_Recycle filespec outvarwinerr
push "${filespec}"
call SHFileOperation_Recycle
!if "${outvarwinerr}" != "s"
pop ${outvarwinerr}
!endif
!macroend
</highlight-nsis>
</highlight-nsis>


[[Category:Disk, Path & File Functions]]
[[Category:Disk, Path & File Functions]]

Latest revision as of 18:11, 26 February 2012

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