How to use the send2trash.plat_win.SHFILEOPSTRUCTW function in Send2Trash

To help you get started, we’ve selected a few Send2Trash examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github hsoft / send2trash / send2trash / plat_win.py View on Github external
def send2trash(path):
    if not isinstance(path, text_type):
        path = text_type(path, 'mbcs')
    if not op.isabs(path):
        path = op.abspath(path)
    path = get_short_path_name(path)
    fileop = SHFILEOPSTRUCTW()
    fileop.hwnd = 0
    fileop.wFunc = FO_DELETE
    # FIX: https://github.com/hsoft/send2trash/issues/17
    # Starting in python 3.6.3 it is no longer possible to use:
    # LPCWSTR(path + '\0') directly as embedded null characters are no longer
    # allowed in strings
    # Workaround
    #  - create buffer of c_wchar[] (LPCWSTR is based on this type)
    #  - buffer is two c_wchar characters longer (double null terminator)
    #  - cast the address of the buffer to a LPCWSTR
    # NOTE: based on how python allocates memory for these types they should
    # always be zero, if this is ever not true we can go back to explicitly
    # setting the last two characters to null using buffer[index] = '\0'.
    buffer = create_unicode_buffer(path, len(path)+2)
    fileop.pFrom = LPCWSTR(addressof(buffer))
    fileop.pTo = None
github braver / SideBarTools / send2trash / plat_win.py View on Github external
def send2trash(path):
		#	
    #if not isinstance(path, str):
    #    path = str(path, 'mbcs')
    #if not op.isabs(path):
    #    path = op.abspath(path)
    fileop = SHFILEOPSTRUCTW()
    fileop.hwnd = 0
    fileop.wFunc = FO_DELETE
    fileop.pFrom = LPCWSTR(path + '\0')
    fileop.pTo = None
    fileop.fFlags = FOF_ALLOWUNDO | FOF_NOCONFIRMATION | FOF_NOERRORUI | FOF_SILENT
    fileop.fAnyOperationsAborted = 0
    fileop.hNameMappings = 0
    fileop.lpszProgressTitle = None
    result = SHFileOperationW(byref(fileop))
    if result:
        msg = "Couldn't perform operation. Error code: %d" % result
        raise OSError(msg)
github aziz / SublimeFileBrowser / dired_file_operations.py View on Github external
def caller(self, mode, sources, destination, duplicate=False):
        '''mode is int: 1 (move), 2 (copy), 3 (delete)'''
        import ctypes
        if ST3: from Default.send2trash.plat_win import SHFILEOPSTRUCTW
        else:   from send2trash.plat_win import SHFILEOPSTRUCTW

        if duplicate:   fFlags = 8
        elif mode == 3: fFlags = 64  # send to recycle bin
        else:           fFlags = 0

        SHFileOperationW = ctypes.windll.shell32.SHFileOperationW
        SHFileOperationW.argtypes = [ctypes.POINTER(SHFILEOPSTRUCTW)]
        pFrom = u'\x00'.join(sources) + u'\x00'
        pTo   = (u'%s\x00' % destination) if destination else None
        wf = ctypes.WINFUNCTYPE(ctypes.wintypes.HWND)
        get_hwnd = wf(ctypes.windll.user32.GetForegroundWindow)
        args  = SHFILEOPSTRUCTW(
                                hwnd   = get_hwnd(),
                                wFunc  = ctypes.wintypes.UINT(mode),
                                pFrom  = ctypes.wintypes.LPCWSTR(pFrom),
                                pTo    = ctypes.wintypes.LPCWSTR(pTo),
                                fFlags = fFlags,
                                fAnyOperationsAborted = ctypes.wintypes.BOOL())
        out = SHFileOperationW(ctypes.byref(args))

        sublime.set_timeout(lambda: emit_event(u'watch_view', self.view.id(), plugin=u'FileBrowserWFS'), 1)
        if not out and destination:  # 0 == success
            sublime.set_timeout(lambda: self.view.run_command('dired_clear_copy_cut_list'), 1)