Send to Recycle Bin: Difference between revisions
From NSIS Wiki
Jump to navigationJump to search
m (Updated author links.) |
No edit summary |
||
(5 intermediate revisions by 3 users not shown) | |||
Line 1: | Line 1: | ||
{ | {{PageAuthor|Anders}} | ||
== Description == | == Description == | ||
Silently send a | Silently send file(s) or a directory to the Windows Recycle bin instead of permanently deleting it. | ||
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> | ||
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 | |||
</highlight-nsis> | </highlight-nsis> | ||
== The Code == | |||
<highlight-nsis> | <highlight-nsis> | ||
!ifndef FO_DELETE | !ifndef FO_DELETE | ||
Line 30: | 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 | Function SHFileOperation_Recycle | ||
Exch $ | Exch $0 | ||
Push $1 | |||
Push $ | 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 | System::Call 'shell32::SHFileOperation(ir0)i.s' | ||
System::Call | System::Free $0 | ||
System::Free $ | Exch | ||
Pop $0 | |||
Pop $ | |||
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]] |
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