News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests

Main Menu

RecycleFile

Started by dedndave, December 19, 2012, 02:04:40 PM

Previous topic - Next topic

dedndave

i found a function on the old forum, written by Hutch, to delete a file into the recycle bin
with a little mod, i suppose it could do folders, as well

it would make a nice addition for masm32 v12   :biggrin:

jj2007

include \masm32\MasmBasic\MasmBasic.inc        ; download
        Init
        Format_C(dont_ask)
        Inkey "bye"
        Exit
end start

;)

dedndave

that makes no sense, Jochen   :biggrin:
i thought basic was a "plain language"
Format_C = delete to recycle bin is somewhat cryptic

here is the meaningful proc in Hutch's code
; This proc calls the SHFileOperation function to delete
; the specified file or files to the recycle bin. Each file
; must be specified with a fully qualified path (otherwise,
; the file will simply be deleted, without being placed in
; the recycle bin). Multiple files can specified by
; separating the individual paths with a null, and appending
; an additional null to the end of the final path.
;
; Returns zero for success, or nonzero for failure.
;
; Initializing the hwnd element to NULL proved to be
; necessary so the SHFileOperation function could delete
; a file that was created by the calling process.

RecycleFile proc pszFullPath:DWORD

    LOCAL   fo      :SHFILEOPSTRUCT

    mov     fo.hwnd,NULL
    mov     fo.wFunc,FO_DELETE
    m2m     fo.pFrom,pszFullPath
    mov     fo.pTo,NULL
    mov     fo.fFlags,FOF_ALLOWUNDO
    invoke  SHFileOperation,ADDR fo
    ret

RecycleFile endp


i haven't played with it, but it may need a little "upgrade" to handle folders
the code is simple enough, but there are a lot of flags to mess with   :P

Donkey

Hi Dave,

You should be able to recursively delete a folder but using only the folder path in SHFILEOPSTRUCT.pFrom, everythng else should remain the same. Make sure to double null terminate the folder name.
"Ahhh, what an awful dream. Ones and zeroes everywhere...[shudder] and I thought I saw a two." -- Bender
"It was just a dream, Bender. There's no such thing as two". -- Fry
-- Futurama

Donkey's Stable

jj2007

Quote from: dedndave on December 19, 2012, 07:30:34 PM
that makes no sense, Jochen   :biggrin:

Wasn't supposed to make sense :biggrin:
Personally, I never use the recycle bin; I always hold Shift when pressing Delete. Maybe because my hard disk is always at the limit, and because all important files are zipped anyway...

dedndave

there are times when i want to programmatically delete files
if they go to the recycle bin, the user has the option of recovering them
it sort of takes away an item of responsibility   :P

Edgar - thanks
didn't know about the double-null   :t

jj2007

I wonder why this doesn't work...

   mov edi, offset buffer
   push edi
   invoke SetLastError, 0
   push eax
   invoke GetDesktopWindow
   invoke SHGetSpecialFolderLocation, eax, CSIDL_BITBUCKET, esp
   call SHGetPathFromIDList

dedndave

HRESULT SHGetSpecialFolderLocation(
  _In_   HWND hwndOwner,
  _In_   int nFolder,
  _Out_  PIDLIST_ABSOLUTE *ppidl
);


we don't know what's going to be in EAX after SetLastError
*ppidl is a pointer to a pointer to a list   :P

C programmers do like their pointers   ::)

GetDesktopWindow seems like a silly function
i thought HWND_DESKTOP = 0

jj2007

Quote from: dedndave on December 20, 2012, 01:44:22 AM
we don't know what's going to be in EAX after SetLastError

we don't even want to know - the push eax is just creating a slot

As a matter of fact, it doesn't work for the BITBUCKET (and a quick look at my desktops confirms there isn't any ::)), but it works for most others (CSIDL_DESKTOPDIRECTORY, for example); sometimes it returns a DOS 8.3 path, sometimes a long one. The short or long path thing is certainly by design, right, Redmond?

dedndave

you may have missed this...

*ppidl is a pointer to a pointer to a list

meaning that the value you point to on the stack needs to be a pointer to the list

   push edi
   push esi
   mov edi, offset buffer
   xor esi,esi
   push edi
   invoke SetLastError, esi
   push esi
   push esp
   invoke SHGetSpecialFolderLocation, esi, CSIDL_BITBUCKET, esp
   pop eax
   call SHGetPathFromIDList
   pop esi
   pop edi


no guarantees   :biggrin:

jj2007

Quote from: dedndave on December 20, 2012, 03:23:13 AM
you may have missed this...
*ppidl is a pointer to a pointer to a list

Dave,
the code posted above worked like a charm from the very beginning, only that apparently I don't have a recycle bin. Here are results for an extended example, source attached (no MasmBasic ;)):

DESKTOPDIRECTORY=       C:\Documents and Settings\user\Desktop
CONTROLS=
DESKTOP=        C:\DOCUME~1\user\Desktop
DRIVES    =
FONTS     =     C:\WINDOWS\Fonts
NETHOOD=        C:\Documents and Settings\user\Risorse di rete
NETWORK=
PERSONAL=       C:\DOCUME~1\user\Documenti
PRINTERS=
PROGRAMS=       C:\Documents and Settings\user\Menu Avvio\Programmi
RECENT   =      C:\Documents and Settings\user\Recent
SENDTO    =     C:\Documents and Settings\user\SendTo
STARTMENU=      C:\Documents and Settings\user\Menu Avvio
STARTUP    =    C:\Documents and Settings\user\Menu Avvio\Programmi\Esecuzione automatica
TEMPLATES=      C:\Documents and Settings\user\Modelli
INTERNET=
ADMINTOOLS=     C:\Documents and Settings\user\Menu Avvio\Programmi\Strumenti di amministrazione
ALTSTARTUP=
APPDATA=        C:\Documents and Settings\user\Dati applicazioni
COMMON_MUSIC=   C:\Documents and Settings\All Users\Documenti\Musica

dedndave

the recycle bin is a strange, unique animal - lol
so is "My Documents" and of course, "Desktop"

1 out of 3 ain't bad   :P

XP MCE2005 SP3
QuoteDESKTOPDIRECTORY=       C:\Documents and Settings\Dave\Desktop
CONTROLS=
DESKTOP=        C:\Documents and Settings\Dave\Desktop
DRIVES    =
FONTS     =     C:\WINDOWS\Fonts
NETHOOD=        C:\Documents and Settings\Dave\NetHood
NETWORK=
PERSONAL=       C:\Documents and Settings\Dave\My Documents
PRINTERS=
PROGRAMS=       C:\Documents and Settings\Dave\Start Menu\Programs
RECENT   =      C:\Documents and Settings\Dave\Recent
SENDTO    =     C:\Documents and Settings\Dave\SendTo
STARTMENU=      C:\Documents and Settings\Dave\Start Menu
STARTUP    =    C:\Documents and Settings\Dave\Start Menu\Programs\Startup
TEMPLATES=      C:\Documents and Settings\Dave\Templates
INTERNET=
ADMINTOOLS=     C:\Documents and Settings\Dave\Start Menu\Programs\Administrative Tools
ALTSTARTUP=
APPDATA=        C:\Documents and Settings\Dave\Application Data
COMMON_MUSIC=   C:\Documents and Settings\All Users\Documents\My Music

TouEnMasm

#12
This one do a folder with a question "do you want to ..."
I have tested it without window

RecycleFile proc pszFullPath:DWORD
LOCAL   fo      :SHFILEOPSTRUCT
Local zchemin[110]:DWORD
lea edx,zchemin
mov ecx,LENGTHOF zchemin
@@:
mov dword ptr [edx],0
dec ecx
jnz @B
invoke lstrcpy,addr zchemin,pszFullPath ;be sure double NULL terminate
mov     fo.hwnd,NULL
mov     fo.wFunc,FO_DELETE
lea edx,zchemin
mov     fo.pFrom,edx
mov     fo.pTo,NULL
mov     fo.fFlags,FOF_ALLOWUNDO
invoke  SHFileOperation,ADDR fo
ret
RecycleFile endp
Fa is a musical note to play with CL

dedndave

the "shotgun" approach is more fun   :biggrin:

start:  xor ebx,ebx

loop00: mov edi, offset buffer
        push edi
        push eax
        invoke SHGetSpecialFolderLocation, 0, ebx, esp
        call SHGetPathFromIDList
        print edi, 13, 10
        inc ebx
        cmp ebx,3Dh
        jbe loop00

        inkey "ok?"
        exit

        end start

dedndave

C:\Documents and Settings\Dave\Desktop

C:\Documents and Settings\Dave\Start Menu\Programs


C:\Documents and Settings\Dave\My Documents
C:\Documents and Settings\Dave\Favorites
C:\Documents and Settings\Dave\Start Menu\Programs\Startup
C:\Documents and Settings\Dave\Recent
C:\Documents and Settings\Dave\SendTo

C:\Documents and Settings\Dave\Start Menu

C:\Documents and Settings\Dave\My Documents\My Music
C:\Documents and Settings\Dave\My Documents\My Videos

C:\Documents and Settings\Dave\Desktop


C:\Documents and Settings\Dave\NetHood
C:\WINDOWS\Fonts
C:\Documents and Settings\Dave\Templates
C:\Documents and Settings\All Users\Start Menu
C:\Documents and Settings\All Users\Start Menu\Programs
C:\Documents and Settings\All Users\Start Menu\Programs\Startup
C:\Documents and Settings\All Users\Desktop
C:\Documents and Settings\Dave\Application Data
C:\Documents and Settings\Dave\PrintHood
C:\Documents and Settings\Dave\Local Settings\Application Data


C:\Documents and Settings\All Users\Favorites
C:\Documents and Settings\Dave\Local Settings\Temporary Internet Files
C:\Documents and Settings\Dave\Cookies
C:\Documents and Settings\Dave\Local Settings\History
C:\Documents and Settings\All Users\Application Data
C:\WINDOWS
C:\WINDOWS\system32
C:\Program Files
C:\Documents and Settings\Dave\My Documents\My Pictures
C:\Documents and Settings\Dave
C:\WINDOWS\system32

C:\Program Files\Common Files

C:\Documents and Settings\All Users\Templates
C:\Documents and Settings\All Users\Documents
C:\Documents and Settings\All Users\Start Menu\Programs\Administrative Tools
C:\Documents and Settings\Dave\Start Menu\Programs\Administrative Tools




C:\Documents and Settings\All Users\Documents\My Music
C:\Documents and Settings\All Users\Documents\My Pictures
C:\Documents and Settings\All Users\Documents\My Videos
C:\WINDOWS\Resources


C:\Documents and Settings\Dave\Local Settings\Application Data\Microsoft\CD Burning