Copying Using the Windows Copy Dialog Box

I am currently using shutil.copy2() to copy a large number of image files and folders (anywhere from 0.5 to 5 gigs). Shutil works fine, but it's so slow. I am wondering if there is a way to transfer this information to Windows to make a copy and provide me with a standard transfer dialog. You know, this guy ...

http://www.top-windows-tutorials.com/images/file-copy.jpg

Many times, my script will take about twice as much time spent on a standard copy of Windows, and it makes me nervous when my python interpreter hangs when the copy starts up. I start the copy process several times and I am looking to shorten the time.

+8
python windows
May 31 '13 at 23:54
source share
3 answers

If your goal is a fantastic copy dialog, SHFileOperation , the Windows API function provides this. The pywin32 package has a python binding for it, ctypes is also an option (for example, "SHFileOperation font types").

Here is my (very slightly tested) example using pywin32:

 import os.path from win32com.shell import shell, shellcon def win32_shellcopy(src, dest): """ Copy files and directories using Windows shell. :param src: Path or a list of paths to copy. Filename portion of a path (but not directory portion) can contain wildcards ``*`` and ``?``. :param dst: destination directory. :returns: ``True`` if the operation completed successfully, ``False`` if it was aborted by user (completed partially). :raises: ``WindowsError`` if anything went wrong. Typically, when source file was not found. .. seealso: `SHFileperation on MSDN <http://msdn.microsoft.com/en-us/library/windows/desktop/bb762164(v=vs.85).aspx>` """ if isinstance(src, basestring): # in Py3 replace basestring with str src = os.path.abspath(src) else: # iterable src = '\0'.join(os.path.abspath(path) for path in src) result, aborted = shell.SHFileOperation(( 0, shellcon.FO_COPY, src, os.path.abspath(dest), shellcon.FOF_NOCONFIRMMKDIR, # flags None, None)) if not aborted and result != 0: # Note: raising a WindowsError with correct error code is quite # difficult due to SHFileOperation historical idiosyncrasies. # Therefore we simply pass a message. raise WindowsError('SHFileOperation failed: 0x%08x' % result) return not aborted 

You can also perform the same copy operation in silent mode (without dialogue, no confirmation, pop-ups with an error) if you set the flags above shellcon.FOF_SILENT | shellcon.FOF_NOCONFIRMATION | shellcon.FOF_NOERRORUI | shellcon.FOF_NOCONFIRMMKDIR. shellcon.FOF_SILENT | shellcon.FOF_NOCONFIRMATION | shellcon.FOF_NOERRORUI | shellcon.FOF_NOCONFIRMMKDIR. See SHFILEOPSTRUCT for details.

+4
Jun 01 '13 at 9:18
source share

See IFileCopy . IFileOperation can be accessed through ctypes and shell32.dll, I'm not sure.

+1
Jun 01 '13 at 0:37
source share

Update: see

It would be nice if it were wrapped in a library ... Using the answers above, I was able to get it to work on windows 7 as follows.

 import pythoncom from win32com.shell import shell,shellcon def win_copy_files(src_files,dst_folder): # @see IFileOperation pfo = pythoncom.CoCreateInstance(shell.CLSID_FileOperation,None,pythoncom.CLSCTX_ALL,shell.IID_IFileOperation) # Respond with Yes to All for any dialog # @see http://msdn.microsoft.com/en-us/library/bb775799(v=vs.85).aspx pfo.SetOperationFlags(shellcon.FOF_NOCONFIRMATION) # Set the destionation folder dst = shell.SHCreateItemFromParsingName(dst_folder,None,shell.IID_IShellItem) for f in src_files: src = shell.SHCreateItemFromParsingName(f,None,shell.IID_IShellItem) pfo.CopyItem(src,dst) # Schedule an operation to be performed # @see http://msdn.microsoft.com/en-us/library/bb775780(v=vs.85).aspx success = pfo.PerformOperations() # @see sdn.microsoft.com/en-us/library/bb775769(v=vs.85).aspx aborted = pfo.GetAnyOperationsAborted() return success and not aborted files_to_copy = [r'C:\Users\jrm\Documents\test1.txt',r'C:\Users\jrm\Documents\test2.txt'] dest_folder = r'C:\Users\jrm\Documents\dst' win_copy_files(files_to_copy,dest_folder) 

The links here were also very useful: http://timgolden.me.uk/pywin32-docs/html/com/win32com/HTML/QuickStartClientCom.html

+1
Nov 14 '13 at 22:27
source share



All Articles