Is there an api call to remove an entire directory tree?
Not just one empty directory. Everything!
T,
Is there an api call to remove an entire directory tree?
Not just one empty directory. Everything!
Like SHFileOperation (https://learn.microsoft.com/en-us/windows/win32/api/shellapi/nf-shellapi-shfileoperationa)
does ?
Be carefull though. Just a little mistake and ....
Regards,
Rudy Wieser
I found that. And it was a bit scary.
T,
I found that. And it was a bit scary.
:-) Which part, the "delete everything" part, or the structure ? In the latter case, its less complex than you might consider it to be :
- - - - - - - - - - - - - - - - - - - - - - - - -
SHFILEOPSTRUCT struc
SHFO_hwnd dd ?
SHFO_wFunc dd ?
SHFO_pFrom dd ? ;must be terminated with a double zero.
SHFO_pTo dd ?
SHFO_fFlags dd ?
SHFO_fAnyOperationsAborted dd ?
SHFO_hNameMappings dd ?
SHFO_lpszProgressTitle dd ?
ends
lea ebx,[@@rSHFO]
call RtlZeroMemory,ebx,size SHFILEOPSTRUCT
mov [ebx].SHFO_wFunc,FO_DELETE
mov [ebx].SHFO_pFrom,offset @@TXT_PathZZ
mov [ebx].SHFO_fFlags,FOF_NOCONFIRMATION or FOF_NOERRORUI or FOF_SILENT
mov [ebx].SHFO_lpszProgressTitle,offset @@TXT_Nul
call SHFileOperationA,ebx
- - - - - - - - - - - - - - - - - - - - - - - - -
Remark: @@TXT_PathZZ must be terminated with a double Zero (it can be used
to provide multiple filenames).
@@TXT_Nul is an empty string.
Hope that helps.
Regards,
Rudy Wieser
Thank you. It will take a bit to sink in
T,
Thank you. It will take a bit to sink in
Youre welcome.
You can leave out the line with "FOF_SILENT", so you will, IIIRC, get a dialog to look at (to confirm your choice)
Suggestion: try it on an empty folder (on an empty or not-really-important thumbdrive perhaps ?). Or better yet, an empty folder you created some testing folders and files* in (using a batch file ?). At least, thats what
I did when I first tested the function.
* perhaps giving one or two the read-only and/or system attributes ? Just to see how the call deals with those (and don't forget to check the returned value).
Regards,
Rudy Wieser
...Suggestion: try it on an empty folder [snip]
Did your test whack files with file locks on them?
T,
...Suggestion: try it on an empty folder [snip]
Did your test whack files with file locks on them?
I can't remember that I ever tried that. When you do you will likely get a "failed" result from the call (and it might stop mid-deleting). Do try !
Regards,
Rudy Wieser
Rats, I was looking for a hammer to replace
FastCopy.exe /CMD=delete /no_confirm_del /balloon=FALSE /auto_close DirectoryPath
T,
Rats, I was looking for a hammer to replace
FastCopy.exe /CMD=delete /no_confirm_del /balloon=FALSE /auto_close
DirectoryPath
I just googled for that program, and found that its deletion method seems to be quite a bit more complex than what SHFileOperation offers. One of the things it does is that it will monitor files it can't directly delete, to delete them when they get closed. IOW, FastCopy could be running for hours(?) if that is how long the to-be-deleted file stays open (logfiles anyone ?)
If-and-when SHFileOperation aborts the deletion process when it encounters the first un-deletable file/folder (as I think it does *) than you could write your own (recursive) directory-traversal method, and just continue
when a file/folder can't be deleted (and, in the end, show a message with
the number of un-deletable files ?).
* I do not see a "continue on failure to delete" flag anywhere.
You would than be left with a pruned folder tree, only holding the un-deletable files. Not quite the wanted result, but way better than a halfway deleted tree, with no indication which file stopped the deletion process.
A possible "hammer" solution could be to use "ShellExecute" or perhaps even "CreateProcess" to run FastCopy.exe. Ugly, but it would work.
Regards,
Rudy Wieser
I have found a way to use robocopy to mirror an empty
directory. It has the result of wiping whatever was
previously there.
SHFileOperation is not enough of a hammer
for my needs. I was hoping so, but ...
You got FastCopy and RoboCopy backwards.
Or I read your sentence backwards -- it
happens.
T,
I have found a way to use robocopy to mirror an empty
directory. It has the result of wiping whatever was
previously there.
:-) There are multiple ways leading to rome.
Though be carefull with external dependancies like that. While FastCopy seems to come with the OS, RoboCopy doesn't.
Hmmm... I just remembered : "RD /S foldername" could/would possibly work as well - though you would need to run it using CMD.exe : "CMD /C RD /S foldername"
But to be honest, I would rather use SHFileOperation. Everything nicely packed in a single OS-provided call. Or even a bit of code walking the directory tree, (and in your case) deleting everything in it (I've used my tree-walking code for in several programs).
Regards,
Rudy Wieser
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 483 |
Nodes: | 16 (2 / 14) |
Uptime: | 179:43:42 |
Calls: | 9,598 |
Calls today: | 4 |
Files: | 13,679 |
Messages: | 6,150,819 |
Posted today: | 1 |