def OnSaveReplay(self, event): stat = self.DemoLoaded() if not stat: return dlg = wx.TextEntryDialog(self, 'Enter the title of your replay','Replay Title') dlg.SetValue(os.path.splitext(self.demo_file.base_name)[0]) if dlg.ShowModal() != wx.ID_OK: return title = dlg.GetValue() dlg.Destroy() stat = self.DemoLoaded() if not stat: return replay_dir = os.path.join(self.tf, r'replay\client\replays') if not os.path.exists(replay_dir): os.makedirs(replay_dir) src = os.path.abspath(self.demo_file.file_name) dest = os.path.abspath(os.path.join(replay_dir, self.demo_file.base_name)) if src.lower() != dest.lower(): pfo = pythoncom.CoCreateInstance(shell.CLSID_FileOperation, None, pythoncom.CLSCTX_ALL, shell.IID_IFileOperation) src_s = shell.SHCreateItemFromParsingName(src, None, shell.IID_IShellItem) dest_s = shell.SHCreateItemFromParsingName(replay_dir, None, shell.IID_IShellItem) pfo.CopyItem(src_s, dest_s) pfo.PerformOperations() rid = demor.last_replay(self.tf) + 1 demor.write_replay(self.tf, rid, self.demo_file, title) dlg = wx.MessageDialog(self, 'New replay clip saved.\nLaunch or restart tf2 to check your replay.', 'Success', wx.ICON_INFORMATION) dlg.ShowModal() dlg.Destroy()
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)
def move(src, dst, flags=shellcon.FOF_NOCONFIRMATION): """ Move files using the built in Windows File copy dialog Requires absolute paths. Does NOT create root destination folder if it doesn't exist. @see http://msdn.microsoft.com/en-us/library/bb775799(v=vs.85).aspx for flags available """ # @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(flags) # Set the destionation folder dst = shell.SHCreateItemFromParsingName(dst, None, shell.IID_IShellItem) if type(src) not in (tuple, list): src = (src, ) for f in src: item = shell.SHCreateItemFromParsingName(f, None, shell.IID_IShellItem) pfo.MoveItem(item, 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 is None and not aborted
def generic_mbed_copy_win(self, source, destination): from win32com.shell import shell, shellcon import pythoncom from os.path import abspath, join from glob import glob for f in glob(join(destination, '*.bin')): os.unlink(f) src = shell.SHCreateItemFromParsingName(source, None, shell.IID_IShellItem) dest_dir = shell.SHCreateItemFromParsingName( abspath(destination), None, shell.IID_IShellItem ) pfo = pythoncom.CoCreateInstance( shell.CLSID_FileOperation, None, pythoncom.CLSCTX_ALL, shell.IID_IFileOperation ) pfo.SetOperationFlags(shellcon.FOF_NO_UI) pfo.CopyItem(src, dest_dir, None, None) pfo.PerformOperations() return True
def _file_operation(src,dst=None,operation='copy',flags=shellcon.FOF_NOCONFIRMATION): # @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(flags) if not isinstance(src,(tuple,list)): src = (src,) if dst is not None: # Set the destination folder dst = shell.SHCreateItemFromParsingName(dst,None,shell.IID_IShellItem) for f in src: item = shell.SHCreateItemFromParsingName(f,None,shell.IID_IShellItem) op = operation.strip().lower() if op=='copy': pfo.CopyItem(item,dst) # Schedule an operation to be performed elif op=='move': pfo.MoveItem(item,dst) elif op=='delete': pfo.DeleteItem(item) else: raise ValueError("Invalid operation {}".format(operation)) # @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 is None and not aborted
def add_to_recent_docs(path): from win32com.shell import shell, shellcon path = unicode(path) app_id = get_app_uid() if app_id is None: shell.SHAddToRecentDocs(shellcon.SHARD_PATHW, path) else: item = shell.SHCreateItemFromParsingName(path, None, shell.IID_IShellItem) shell.SHAddToRecentDocs(shellcon.SHARD_APPIDINFO, (item, app_id))
def send2trash(paths): if not isinstance(paths, list): paths = [paths] # convert data type paths = [ text_type(path, "mbcs") if not isinstance(path, text_type) else path for path in paths ] # convert to full paths paths = [ op.abspath(path) if not op.isabs(path) else path for path in paths ] # remove the leading \\?\ if present paths = [ path[4:] if path.startswith("\\\\?\\") else path for path in paths ] # create instance of file operation object fileop = pythoncom.CoCreateInstance( shell.CLSID_FileOperation, None, pythoncom.CLSCTX_ALL, shell.IID_IFileOperation, ) # default flags to use flags = (shellcon.FOF_NOCONFIRMATION | shellcon.FOF_NOERRORUI | shellcon.FOF_SILENT | shellcon.FOFX_EARLYFAILURE) # determine rest of the flags based on OS version # use newer recommended flags if available if int(version().split(".", 1)[0]) >= 8: flags |= ( 0x20000000 # FOFX_ADDUNDORECORD win 8+ | 0x00080000 # FOFX_RECYCLEONDELETE win 8+ ) else: flags |= shellcon.FOF_ALLOWUNDO # set the flags fileop.SetOperationFlags(flags) # actually try to perform the operation, this section may throw a # pywintypes.com_error which does not seem to create as nice of an # error as OSError so wrapping with try to convert sink = CreateSink() try: for path in paths: item = shell.SHCreateItemFromParsingName(path, None, shell.IID_IShellItem) fileop.DeleteItem(item, sink) result = fileop.PerformOperations() aborted = fileop.GetAnyOperationsAborted() # if non-zero result or aborted throw an exception if result or aborted: raise OSError(None, None, paths, result) except pywintypes.com_error as error: # convert to standard OS error, allows other code to get a # normal errno raise OSError(None, error.strerror, path, error.hresult)
def test_parsing_name(self): sf = shell.SHGetDesktopFolder() flags = shellcon.SHCONTF_FOLDERS | shellcon.SHCONTF_NONFOLDERS children = sf.EnumObjects(0, flags) child_pidl = children.next() name = sf.GetDisplayNameOf(child_pidl, shellcon.SHGDN_FORPARSING) item = shell.SHCreateItemFromParsingName(name, None, shell.IID_IShellItem) # test the name we get from the item is the same as from the folder. self.assertEqual(name, item.GetDisplayName(shellcon.SHGDN_FORPARSING))
def file_operation(items: List[str], op: str, dst: str = "") -> bool: # create an instance of IShellItem for the destination folder destination = None if op in [Fo.COPY, Fo.MOVE]: dst = dst.replace('/', '\\') destination = shell.SHCreateItemFromParsingName( dst, None, shell.IID_IShellItem) # create IShellItemArray from items sia = __create_shell_item_array(items) # create an instance of IFileOperation fo = pythoncom.CoCreateInstance(shell.CLSID_FileOperation, None, pythoncom.CLSCTX_ALL, shell.IID_IFileOperation) # queue the operation op = op.lower() if op == Fo.COPY: fo.CopyItems(sia, destination) elif op == Fo.MOVE: fo.MoveItems(sia, destination) elif op == Fo.RECYCLE: fo.DeleteItems(sia) elif op == Fo.DELETE: # if SetOperationFlags is not called, the default value used by the operation # is FOF_ALLOWUNDO | FOF_NOCONFIRMMKDIR fo.SetOperationFlags(shellcon.FOF_NOCONFIRMMKDIR) fo.DeleteItems(sia) else: return False # commit try: fo.PerformOperations() except pythoncom.pywintypes.com_error as e: # COPYENGINE_E_USER_CANCELLED = 0x80270000 (-2144927744) - User canceled the current action if __hr(e.hresult) != shellcon.COPYENGINE_E_USER_CANCELLED: print(e, flush=True) return True
def recycle(path): path = os.path.join(win32api.GetShortPathName(os.path.split(path)[0]), os.path.split(path)[1]) if len(path) > 259: path = win32api.GetShortPathName(path) if path.startswith("\\\\?\\") and len(path) < 260: path = path[4:] if (hasattr(shell, "CLSID_FileOperation") and hasattr(shell, "IID_IFileOperation")): # Vista and later fo = pythoncom.CoCreateInstance(shell.CLSID_FileOperation, None, pythoncom.CLSCTX_ALL, shell.IID_IFileOperation) fo.SetOperationFlags(shellcon.FOF_ALLOWUNDO | shellcon.FOF_NOCONFIRMATION | shellcon.FOF_RENAMEONCOLLISION | shellcon.FOF_SILENT) try: item = shell.SHCreateItemFromParsingName( path, None, shell.IID_IShellItem) fo.DeleteItem(item) success = fo.PerformOperations() is None aborted = fo.GetAnyOperationsAborted() except pythoncom.com_error as exception: raise TrashAborted(-1) else: # XP retcode, aborted = shell.SHFileOperation( (0, shellcon.FO_DELETE, path, "", shellcon.FOF_ALLOWUNDO | shellcon.FOF_NOCONFIRMATION | shellcon.FOF_RENAMEONCOLLISION | shellcon.FOF_SILENT, None, None)) success = retcode == 0 if aborted: raise TrashAborted(aborted) return success and not aborted