def callback(smb_fs: smbfs.SMBFS): for walk_path in smb_fs.walk.dirs(path, max_depth=1): if (smb_fs.exists(walk_path)): info = smb_fs.getinfo(walk_path) find_data = wintypes.WIN32_FIND_DATAW() find_data.dwFileAttributes = 16 find_data.cFileName = info.name argus[1](pointer(find_data), argus[2]) for walk_path in smb_fs.walk.files(path, max_depth=1): if (smb_fs.exists(walk_path)): info = smb_fs.getinfo(walk_path) filesize = smb_fs.getsize(walk_path) find_data = wintypes.WIN32_FIND_DATAW() find_data.dwFileAttributes = 32 find_data.cFileName = info.name if (len(info.name) >= 11): info_cut = info.name.split(".") cAlternateFileName = info_cut[0][ 0:6] + "~1." + info.suffix[0:3] else: find_data.cAlternateFileName = info.name find_data.ftCreationTime = wintypes.FILETIME() find_data.ftLastAccessTime = wintypes.FILETIME() find_data.ftLastWriteTime = wintypes.FILETIME() find_data.nFileSizeHigh = wintypes.DWORD(0) find_data.nFileSizeLow = wintypes.DWORD(filesize) find_data.dwReserved0 = wintypes.DWORD(0) find_data.dwReserved1 = wintypes.DWORD(0) argus[1](pointer(find_data), argus[2])
def listdir_stat(dirname='.', glob=None): dirname = os.path.abspath(dirname) filename = os.path.join(dirname, glob or '*') data = wintypes.WIN32_FIND_DATAW() data_p = ctypes.byref(data) handle = _FindFirstFile(filename, data_p) if handle == INVALID_HANDLE_VALUE: error = ctypes.GetLastError() if error == ERROR_FILE_NOT_FOUND: return raise ctypes.WinError() try: while True: if data.cFileName not in ('.', '..'): st = _find_data_to_stat(data) yield (data.cFileName, st) success = _FindNextFile(handle, data_p) if not success: error = ctypes.GetLastError() if error == ERROR_NO_MORE_FILES: break raise ctypes.WinError() finally: if not _FindClose(handle): raise ctypes.WinError()
def os_listdir(path): data = wintypes.WIN32_FIND_DATAW() data_p = ctypes.byref(data) filename = os.path.join(path, '*') handle = scandir.FindFirstFile(filename, data_p) if handle == scandir.INVALID_HANDLE_VALUE: error = ctypes.GetLastError() if error == scandir.ERROR_FILE_NOT_FOUND: return [] raise scandir.win_error(error, path) names = [] try: while True: name = data.cFileName if name not in ('.', '..'): names.append(name) success = scandir.FindNextFile(handle, data_p) if not success: error = ctypes.GetLastError() if error == scandir.ERROR_NO_MORE_FILES: break raise scandir.win_error(error, path) finally: if not scandir.FindClose(handle): raise scandir.win_error(ctypes.GetLastError(), path) return names
def scandir(path='.', windows_wildcard='*.*'): # Call FindFirstFile and handle errors data = wintypes.WIN32_FIND_DATAW() data_p = ctypes.byref(data) filename = _join(path, windows_wildcard) handle = FindFirstFile(filename, data_p) if handle == INVALID_HANDLE_VALUE: error = ctypes.GetLastError() if error == ERROR_FILE_NOT_FOUND: # No files, don't yield anything return raise win_error(error, path) # Call FindNextFile in a loop, stopping when no more files try: while True: # Skip '.' and '..' (current and parent directory), but # otherwise yield (filename, stat_result) tuple name = data.cFileName if name not in ('.', '..'): yield DirEntry(path, name, None, find_data_to_stat(data)) success = FindNextFile(handle, data_p) if not success: error = ctypes.GetLastError() if error == ERROR_NO_MORE_FILES: break raise win_error(error, path) finally: if not FindClose(handle): raise win_error(ctypes.GetLastError(), path)
def folderCount(filePath): try: filePath = unicode(filePath) FILE_ATTRIBUTE_DIRECTORY = 0x10 FindFirstFile = windll.kernel32.FindFirstFileW FindNextFile = windll.kernel32.FindNextFileW FindClose= ctypes.windll.kernel32.FindClose out = wintypes.WIN32_FIND_DATAW() fldr = FindFirstFile(filePath,byref(out)) i=0 while True: ishide = out.dwFileAttributes & (FILE_ATTRIBUTE_SYSTEM ) if not ishide and out.cFileName not in [u'.',u'..']: isdir = out.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY # print out.cFileName,isdir if isdir: i+=1 FindClose(fldr) return 1 if not FindNextFile(fldr,byref(out)): break FindClose(fldr) return i except Exception,e: print e return 0
def windows_enumerate_directory(directory, files): from ctypes import windll, wintypes, byref FILE_ATTRIBUTE_DIRECTORY = 0x10 INVALID_HANDLE_VALUE = -1 BAN = ('.', '..') FindFirstFile = windll.kernel32.FindFirstFileW FindNextFile = windll.kernel32.FindNextFileW FindClose = windll.kernel32.FindClose out = wintypes.WIN32_FIND_DATAW() fldr = FindFirstFile(os.path.join(directory, "*"), byref(out)) if fldr == INVALID_HANDLE_VALUE: raise ValueError("invalid handle!") try: while True: if out.cFileName not in BAN: isdir = out.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY if isdir: windows_enumerate_directory( os.path.join(directory, out.cFileName), files) else: files.append(os.path.join(directory, out.cFileName)) if not FindNextFile(fldr, byref(out)): break finally: FindClose(fldr)
def islink(path): if not os.path.isdir(path): return False data = wintypes.WIN32_FIND_DATAW() kernel32.FindClose(kernel32.FindFirstFileW(path, ctypes.byref(data))) if not data.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT: return False return IsReparseTagNameSurrogate(data.dwReserved0)
def _scandir_python(path=unicode('.')): """Like os.listdir(), but yield DirEntry objects instead of returning a list of names. """ # Call FindFirstFile and handle errors if isinstance(path, bytes): is_bytes = True filename = join(path.decode('mbcs', 'strict'), '*.*') else: is_bytes = False filename = join(path, '*.*') data = wintypes.WIN32_FIND_DATAW() data_p = ctypes.byref(data) handle = FindFirstFile(filename, data_p) if handle == INVALID_HANDLE_VALUE: error = ctypes.GetLastError() if error == ERROR_FILE_NOT_FOUND: # No files, don't yield anything return raise win_error(error, path) # Call FindNextFile in a loop, stopping when no more files try: while True: # Skip '.' and '..' (current and parent directory), but # otherwise yield (filename, stat_result) tuple name = data.cFileName if name not in ('.', '..'): if is_bytes: name = name.encode('mbcs', 'replace') yield Win32DirEntryPython(path, name, data) data = wintypes.WIN32_FIND_DATAW() data_p = ctypes.byref(data) success = FindNextFile(handle, data_p) if not success: error = ctypes.GetLastError() if error == ERROR_NO_MORE_FILES: break raise win_error(error, path) finally: if not FindClose(handle): raise win_error(ctypes.GetLastError(), path)
def dir(filePath,fileFilter=lambda x:True,mask='*.*'): filePath = dospath(clearup(filePath)) if mask: searchPath = unicode('%s\\%s' % (filePath,mask)) else: searchPath = unicode('%s' % (filePath)) try: FILE_ATTRIBUTE_DIRECTORY = 0x10 FindFirstFile = windll.kernel32.FindFirstFileW FindNextFile = windll.kernel32.FindNextFileW FindClose= ctypes.windll.kernel32.FindClose out = wintypes.WIN32_FIND_DATAW() fldr = FindFirstFile(searchPath,byref(out)) fileList = [] folderList = [] while True: ishide = out.dwFileAttributes & (FILE_ATTRIBUTE_SYSTEM ) if not ishide and out.cFileName not in [u'.',u'..']: isdir = out.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY f = {} f['sz'] = (out.nFileSizeHigh << 32) + out.nFileSizeLow f['ct'] = convert_filetime(out.ftCreationTime) f['at'] = convert_filetime(out.ftLastAccessTime) f['wt'] = convert_filetime(out.ftLastWriteTime) f['fn'] = out.cFileName f['path'] = filePath f['uri'] = '%s\\%s' %(f['path'],f['fn']) if isdir: f['tp'] = FolderType f['ext'] = '<DIR>' f['emy']=(windll.Shlwapi.PathIsDirectoryEmptyW(f['uri'])==1) folderList.append(f) else: lf,ext = os.path.splitext(f['fn']) f['ext'] = ext f['tp'] = FileType if fileFilter(f): fileList.append(f) if not FindNextFile(fldr,byref(out)): break FindClose(fldr) except Exception,e: #print searchPath,e return [],[]
def scandir(directory='.'): """Like os.listdir(), but yield DirEntry objects instead of returning a list of names. """ # Call FindFirstFile and handle errors data = wintypes.WIN32_FIND_DATAW() data_p = ctypes.byref(data) filename = join(directory, '*.*') handle = FindFirstFile(filename, data_p) if handle == INVALID_HANDLE_VALUE: error = ctypes.GetLastError() if error == ERROR_FILE_NOT_FOUND: # No files, don't yield anything return raise win_error(error, directory) # Call FindNextFile in a loop, stopping when no more files try: while True: # Skip '.' and '..' (current and parent directory), but # otherwise yield (filename, stat_result) tuple name = data.cFileName if name not in ('.', '..'): yield Win32DirEntry(directory, name, data) data = wintypes.WIN32_FIND_DATAW() data_p = ctypes.byref(data) success = FindNextFile(handle, data_p) if not success: error = ctypes.GetLastError() if error == ERROR_NO_MORE_FILES: break raise win_error(error, directory) finally: if not FindClose(handle): raise win_error(ctypes.GetLastError(), directory)