简体   繁体   English

有没有办法在所有硬盘驱动器上进行文件观察?

[英]Is there a way to file watcher on all hard disk drives?

I have a scrip to detect some files created on Hard disk drive using win32file API with python我有一个脚本来检测使用win32file API 和 python 在硬盘驱动器上创建的一些文件

from win32file import CreateFile, ReadDirectoryChangesW
import win32con

def Watcher():
        ExtensionScan = ['exe', 'dll', 'vbs', 'com', 'jar']
        FILE_LIST_DIRECTORY = 0x0001
        path_to_watch = "c:\\"
        hDir = CreateFile(
            path_to_watch,
            FILE_LIST_DIRECTORY,
            win32con.FILE_SHARE_READ | 
            win32con.FILE_SHARE_WRITE | 
            win32con.FILE_SHARE_DELETE,
            None,
            win32con.OPEN_EXISTING,
            win32con.FILE_FLAG_BACKUP_SEMANTICS,
            None
        )
        while True:
            Results = ReadDirectoryChangesW (
                hDir,
                1024,
                True,
                win32con.FILE_NOTIFY_CHANGE_FILE_NAME |
                win32con.FILE_NOTIFY_CHANGE_DIR_NAME |
                win32con.FILE_NOTIFY_CHANGE_ATTRIBUTES |
                win32con.FILE_NOTIFY_CHANGE_SIZE |
                win32con.FILE_NOTIFY_CHANGE_LAST_WRITE |
                win32con.FILE_NOTIFY_CHANGE_SECURITY,
                None,
                None
            )
            for Action, File in Results:
                FullPath = "{}\\{}".format(path_to_watch, File).lower()
                Extension = FullPath.split('.')[-1]
                if Action == 1 and Extension in ExtensionScan:
                    Name = FullPath.split('\\')[-1]
                    print({FullPath: {"Name": Name, "Extension": Extension}})

This code works, but i want watcher on all hard disk not specific path此代码有效,但我希望所有硬盘上的观察者不是特定路径

path_to_watch = "c:\" path_to_watch = "c:\"

I have an idea, call Watcher(drive="D:\\") with a specific drive by threading for all drives like below code:我有一个想法,通过对所有驱动器进行线程化,使用特定驱动器调用Watcher(drive="D:\\") ,如下面的代码:

Drives = ["C:\\", "D:\\", "E:\\"]

for i in Drives:
  Thread(target=Watcher, args=(i,).start()

but this solution is not good for processor performance但是这种解决方案对处理器性能不利

Finally, is there a way build in win32file for this solution or use my idea?最后,有没有办法为这个解决方案构建win32file或使用我的想法?

Thanks all谢谢大家

def Watcher(_disk):
        ExtensionScan = ['exe', 'dll', 'vbs', 'com', 'jar']
        FILE_LIST_DIRECTORY = 0x0001
        path_to_watch = _disk
...

for i in Drives:
     Thread(target=Watcher(i)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM