Sysop: | Amessyroom |
---|---|
Location: | Fayetteville, NC |
Users: | 26 |
Nodes: | 6 (0 / 6) |
Uptime: | 78:39:59 |
Calls: | 482 |
Calls today: | 1 |
Files: | 1,072 |
Messages: | 97,166 |
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
T,
SHFileOperation is not enough of a hammer
for my needs. I was hoping so, but ...
In what way its not enough ? Be carefull if RoboCopy seems to delete locked files too - a waiting for a file to become unlocked might give some side effects* you need to keep aware of.
* like a folder list upto the to-be-deleted file you should not try to put a new file in - it could easily disappear when RoboCopy finishes up (something to check?).
You got FastCopy and RoboCopy backwards.
Or I read your sentence backwards -- it
happens.
Neither. I interpreted your "Rats, I was looking for a hammer to replace"
as that you already had a FastCopy solution, but (where writing a program where you) wanted to get rid of it in favour of an API call.
An interpretation which definitily isn't /my/ fault ofcourse. :-) <whistle>
Regards,
Rudy Wieser
Here is what is really annoying, I can delete the files left
behind with Windows Explorer. The are no locks or permission
issues on them.
This is just a flaw in Windows. And why I am after a hammer.
I was hoping to get rid of the Fast Copy and Robo Copy solution
and use an API call instead.
Since I also use Fast Copy to copy the archives over, I
think I will just stick with Fast Copy's hammer.
Fast Copy is danged fast!
Thank you for all the most gracious help!
It might be that you need to elevate yourself to admin
It might be that you need to elevate yourself to admin
My program runs as Administrator.
I am able to delete the strays with a user account
with Windows Explorer
here is a possibility
that your program has (temporary) files open while you are trying to delete the folder-tree they are in
If you ever find out why some files would not delete than please do tell. I >could use the knowledge. :-)