Link to home
Start Free TrialLog in
Avatar of MCHomes
MCHomes

asked on

SHFileOperation - Skipping I/O File Error on Delete

I'm using SHFileOperation to delete multiple files and from time to time I will get a i/o file error while deleting files that may be opened by other programs.

When SHFileOperation encounters a i/o file error, it usually exits with an error, without completing the remaining delete operation, even with FOF_NOERRORUI flag set. I need to know how to get this function to skip those files that cannot be deleted, and continue deleting the remaining files passed to the function. If not, is there a way of determining which file caused the i/o error?

As you will see in my code below, I'm passing multiple files to the function, but not all from the same folder. The reason for this is I want the whole delete operation to be displayed in the same progress window.


'''''''''''''''''''''''''''''''''''''''''''''

    lenFileop = LenB(fileop)    ' double word alignment increase the
    ReDim foBuf(1 To lenFileop) ' size of the structure.
   
    'Set all of the delete parameters
    With fileop
        .hWnd = hWnd
        .wFunc = FO_DELETE
        .fFlags = FOF_NOERRORUI
       
        ' multiple source files passed to function
         Src$ = "c:\Temp1\file1.tmp" & vbNullChar & "c:\Temp2\file2.tmp" & vbNullChar & "c:\Temp3\file3.tmp"
         
        .pFrom = Src$ & vbNullChar & vbNullChar & vbNullChar

    End With

    ' Now we copy the structure into a byte array
    Call CopyMemory(foBuf(1), fileop, lenFileop)
    ' Next we move the last 12 bytes by 2 to byte align the data
    Call CopyMemory(foBuf(19), foBuf(21), 12)
   
    lRet = SHFileOperation(foBuf(1))

'''''''''''''''''''''''''''''''''''''''''
Avatar of Anthony Perkins
Anthony Perkins
Flag of United States of America image

Please maintain your open questions:
Copy contents from listbox to a second listbox using API Date: 04/02/2002 09:30AM PST
https://www.experts-exchange.com/questions/20284119/Copy-contents-from-listbox-to-a-second-listbox-using-API.html
Hiding the Focus Rect in a Listbox Date: 03/24/2002 09:52PM PST
https://www.experts-exchange.com/questions/20281040/Hiding-the-Focus-Rect-in-a-Listbox.html

The following questions should be deleted as there have not been any contributions:
Retrieving explorer shell info from my custom Toolbar Date: 07/02/2002 11:45PM PST
https://www.experts-exchange.com/questions/20318980/Retrieving-explorer-shell-info-from-my-custom-Toolbar.html
Determining files highlighted in a shell window Date: 06/10/2002 05:50PM PST  
https://www.experts-exchange.com/questions/20310067/Determining-files-highlighted-in-a-shell-window.html

Thanks,
Anthony
Couldn't you manage with On error... statement?
Avatar of MCHomes
MCHomes

ASKER

On Error staement? i/o error is called by the shell not VB. I've tried everything I can think of to figure out which file causes this error. It sure would be nice if SHFileOperation had a FOF_SKIPONFILEERROR flag, but they don't.
It doesn't look like you are using the flags to move to recycle bin, so why not just use the kill statement and trap errors on that?

on error resume next
kill  "c:\Temp1\file1.tmp"
kill "c:\Temp2\file2.tmp"
kill "c:\Temp3\file3.tmp"
on error goto 0

This will delete whichever files can be deleted while leaving the ones that cannot.
You can't fix the SHFileOperation API's behaviour, by the way.  You can see the same behaviour in Windows Explorer.  It's irritating.
Avatar of MCHomes

ASKER

PaulHews,

Of course the Kill statement doesn't show any system progress dialog. btw, I do need recursive deleting capability via a ContextMenu in my app. As you mentioned, the Windows Explorer behavior is irritating, so I was hoping to find a way around it. If I can some how figure out which file caused the error, I'm pretty much home free. Maybe a message hook might be the answer, but that may be harder said than done.
ASKER CERTIFIED SOLUTION
Avatar of kishore1977
kishore1977

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Just take in mind one thing, IMHO, if OS cannot do it, sure we can't with code.
A request has been made to delete this question; if there are no objections within 72 hours, the request will be granted.

EXPERTS: Please leave your comments on this matter here.

Netminder
EE Admin
Avatar of MCHomes

ASKER

Thanks kishore.