Home > Back-end >  How can I get files dir from opened several foleders in python
How can I get files dir from opened several foleders in python

Time:11-27

I trying to make that gather files from several file explorrer with Python

I want to move many files to one folder from already opend foleders by file explorer

How can I approch opend directory?

Manual typing dir one by one is not an option

so, what I want is I want to get several dir path from opend file explorer

CodePudding user response:

Here is the code I use to get a list of file names that have been copied to the clipboard from Windows Explorer:

import ctypes
import struct

from ctypes.wintypes import BOOL, HWND, HANDLE, HGLOBAL, UINT, LPVOID
from ctypes import c_size_t as SIZE_T

OpenClipboard = ctypes.windll.user32.OpenClipboard
OpenClipboard.argtypes = HWND,
OpenClipboard.restype = BOOL
EmptyClipboard = ctypes.windll.user32.EmptyClipboard
EmptyClipboard.restype = BOOL
GetClipboardData = ctypes.windll.user32.GetClipboardData
GetClipboardData.argtypes = UINT,
GetClipboardData.restype = HANDLE
CloseClipboard = ctypes.windll.user32.CloseClipboard
CloseClipboard.restype = BOOL
IsClipboardFormatAvailable = ctypes.windll.user32.IsClipboardFormatAvailable
IsClipboardFormatAvailable.argtypes = UINT,
IsClipboardFormatAvailable.restype = BOOL
CF_HDROP = 15

GlobalAlloc = ctypes.windll.kernel32.GlobalAlloc
GlobalAlloc.argtypes = UINT, SIZE_T
GlobalAlloc.restype = HGLOBAL
GlobalLock = ctypes.windll.kernel32.GlobalLock
GlobalLock.argtypes = HGLOBAL,
GlobalLock.restype = LPVOID
GlobalUnlock = ctypes.windll.kernel32.GlobalUnlock
GlobalUnlock.argtypes = HGLOBAL,
GlobalSize = ctypes.windll.kernel32.GlobalSize
GlobalSize.argtypes = HGLOBAL,
GlobalSize.restype = SIZE_T
GMEM_MOVEABLE = 0x0002
GMEM_ZEROINIT = 0x0040
GMEM_SHARE    = 0x2000
GHND = GMEM_MOVEABLE | GMEM_ZEROINIT

def read_raw(fmt):
    handle = GetClipboardData(fmt)
    pcontents = GlobalLock(handle)
    size = GlobalSize(handle)
    raw_string = None
    if pcontents and size:
        raw_data = ctypes.create_string_buffer(size)
        ctypes.memmove(raw_data, pcontents, size)
        raw_string = raw_data.raw
    GlobalUnlock(handle)
    return raw_string

def get():
    OpenClipboard(None)
    if IsClipboardFormatAvailable(CF_HDROP):
        raw_string = read_raw(CF_HDROP)
        CloseClipboard()
        pFiles, pt_x, pt_y, fNC, fWide = struct.unpack('IIIII', raw_string[:20])
        cooked = raw_string[pFiles:].decode('utf-16' if fWide else 'mbcs')
        return [name for name in cooked.split(u'\0') if name]
  • Related