How to use IFileOperation from ctypes

Sure, it’s located in pythoncom and shell for constants, for example:

from win32com.shell import shell
import pythoncom

# create an instance of IFileOperation
fo = pythoncom.CoCreateInstance(shell.CLSID_FileOperation, None, pythoncom.CLSCTX_ALL, shell.IID_IFileOperation)

# here you can use SetOperationFlags, progress Sinks, etc.

# create an instance of IShellItem for the source item
item1 = shell.SHCreateItemFromParsingName("c:\\temp\\source.txt", None, shell.IID_IShellItem)

# create an instance of IShellItem for the target folder
folder = shell.SHCreateItemFromParsingName("c:\\another", None, shell.IID_IShellItem)

# queue the copy operation
fo.CopyItem(item1, folder, "new name.txt", None)

# commit
fo.PerformOperations()

Leave a Comment