Search code examples
pythonwindowscompywin32file-copying

How to automate "Retry" on error when copying a file with the Windows Explorer COM model?


I'm automating specific copy operations (that can't be done via normal filesystem copy) with Windows Explorer COM:

import pythoncom
from win32comext.shell import shell
fo = pythoncom.CoCreateInstance(shell.CLSID_FileOperation, None, pythoncom.CLSCTX_ALL, shell.IID_IFileOperation)
fo.CopyItem(src, dest)
fo.PerformOperations()

Once every thousands of files, I get:

Error 0x802a0006

UI_E_VALUE_NOT_DETERMINED 0x802A0006 The requested value cannot be determined.

Doing "Retry" and checking the checkbox "Do this for future files" on the dialog box does not help for future files, so it's a problem when automating.

Question:

1. Does using shell.FOF_NOCONFIRMATION make the dialog choose automatically "Retry", or "Ignore" or "Cancel"? Which one is correct?

2. Is there a way to automate retrying without dialog? Without doing manually with a for _ in range(MAX_RETRIES): try: ... except: ...

enter image description here


Solution

  • To make your automation smooth you can suppress guı

       fo.SetOperationFlags(
            shellcon.FOF_NOCONFIRMATION |  # Suppress confirmation dialogs
            shellcon.FOF_SILENT |  # Suppress UI
            shellcon.FOF_NOERRORUI |  # Suppress error dialogs
            shellcon.FOF_NOCONFIRMMKDIR  # Create directories automatically
        )
    

    This will help you move without interreption by gui

    When error happens

        try:
            # do copying
        except pythoncom.com_error as e:
            # when error happens
    

    You can detect it like so, but to decide what to do with your files is dependet on problem. You can retry for few times and ignore if you wish. But take note of which file it raises arror and put it isnide a log.txt so you can manually deal with problemantic ones. The error code you provide is expalained as corrupted file so it might not be useful to retrying it over and over.