找回密码
 加入
搜索
查看: 1736|回复: 11

如何被动知道一个文件是否被创建 而不是主动扫描

[复制链接]
发表于 2008-7-22 13:03:21 | 显示全部楼层 |阅读模式
比如文件 1.txt 本来没有 被其他创建了后 有什么办法能通知脚本 让程序知道这文件被创建了

一般的的解决方法是用循环 不停的扫描目录 以发现1.txt存在 存在即被创建了

但这样需要脚本不停的用循环执行FileExists 如果长时间运行 会很耗cpu

请问高人有解决之道吗

[ 本帖最后由 bluefog 于 2008-7-25 11:29 编辑 ]
发表于 2008-7-22 13:32:10 | 显示全部楼层
试下这个,忘记原作者了。
[au3]$strComputer = "."
$objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\cimv2")

$colMonitoredEvents = $objWMIService.ExecNotificationQuery _
("SELECT * FROM __InstanceOperationEvent WITHIN 5 WHERE " _
& "Targetinstance ISA 'CIM_DirectoryContainsFile' and " _
& "TargetInstance.GroupComponent= " _
& "'Win32_Directory.Name=""d:\\\\WINDOWS""'")

While 1
$objEventObject = $colMonitoredEvents.NextEvent()

Select
Case $objEventObject.Path_.Class()="__InstanceCreationEvent"
ConsoleWrite ("一个新的文件被创建: " & $objEventObject.TargetInstance.PartComponent() & @CR)
Case $objEventObject.Path_.Class()="__InstanceDeletionEvent"
ConsoleWrite ("一个旧的文件被删除: " & $objEventObject.TargetInstance.PartComponent() & @CR)
EndSelect
WEnd[/au3]
发表于 2008-7-22 13:36:18 | 显示全部楼层
似乎还是个循环,只是这个方法很巧妙的。
 楼主| 发表于 2008-7-22 13:41:41 | 显示全部楼层
谢谢bing614
研究一下
 楼主| 发表于 2008-7-22 13:45:06 | 显示全部楼层
另外请问楼上2位高人 我这段代码怎么执行没有结果啊  my.ini是肯定存在的
但程序运行什么都没有反馈
Func fxwj()
If FileExists("d:\my.ini") Then
    MsgBox(4096, "my.ini", "存在")
EndIf
EndFunc

AdlibEnable("fxwj",30000)

非常感谢

[ 本帖最后由 bluefog 于 2008-7-22 14:09 编辑 ]
发表于 2008-7-22 13:53:21 | 显示全部楼层
("d:my.ini") ===("d:\my.ini")
 楼主| 发表于 2008-7-22 14:01:35 | 显示全部楼层
原程序是对的 考过来编辑时候出了点错

AdlibEnable("fxwj",30000) 没有任何结果

而单独用 fxwj() 就有结果
 楼主| 发表于 2008-7-22 14:30:12 | 显示全部楼层
2楼的代码实在看不懂 不知道该如何改
汗一个
 楼主| 发表于 2008-7-22 15:13:02 | 显示全部楼层
$zd=0
While $zd=0
;AdlibEnable("fxwj",3000)
If FileExists("d:\my.ini") Then
  $zd=1
Else
  $zd=0
EndIf
Sleep(1000)
WEnd

用这个办法解决了 cpu占用在0-3之间 还是可以接受的
再次感谢楼上二位
发表于 2008-7-22 15:23:55 | 显示全部楼层
你的代码缺少等待,运行了就结束了,所以要给予一定的等待时间才行。
HotKeySet("{Esc}","_EXit")
AdlibEnable("fxwj", 5000)
Func fxwj()
        If FileExists("d:\my.ini") Then
                MsgBox(4096, "my.ini", "存在")
        EndIf
EndFunc   ;==>fxwj

Func _exit()
        Exit
EndFunc

While 1
        sleep(100)
WEnd
 楼主| 发表于 2008-7-22 15:32:29 | 显示全部楼层
谢谢顽固不化 找到了关键所在 调试通过
发表于 2010-3-18 10:48:08 | 显示全部楼层
$strComputer = "."
$objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\cimv2")

$colMonitoredEvents = $objWMIService.ExecNotificationQuery _
("SELECT * FROM __InstanceOperationEvent WITHIN 5 WHERE " _
& "Targetinstance ISA 'CIM_DirectoryContainsFile' and " _
& "TargetInstance.GroupComponent= " _
& "'Win32_Directory.Name=""C:\\\\Documents and Settings\\\\Administrator\\\\桌面""'")

While 1
$objEventObject = $colMonitoredEvents.NextEvent()
Select
Case $objEventObject.Path_.Class()="__InstanceCreationEvent"
Automsg("新建文件:" & $objEventObject.TargetInstance.PartComponent() & @CR)
Case $objEventObject.Path_.Class()="__InstanceDeletionEvent"
Automsg("删除文件:" & $objEventObject.TargetInstance.PartComponent() & @CR)
EndSelect
WEnd

Func Automsg($Automsg)
$Automsg2 = StringLeft($Automsg, 5)
$Automsg = StringTrimLeft($Automsg, StringInStr($Automsg, "="))
if StringLeft($Automsg, 1) = '"' then $Automsg = StringTrimLeft($Automsg, 1)
do
$Automsg = StringTrimRight($Automsg, 1)
Until StringRight($Automsg, 1) = '"'
$Automsg = StringTrimRight($Automsg, 1)
msgbox(0,"目录监视事件",$Automsg2 & "  " & $Automsg)
EndFunc


这样就好理解了
您需要登录后才可以回帖 登录 | 加入

本版积分规则

QQ|手机版|小黑屋|AUTOIT CN ( 鲁ICP备19019924号-1 )谷歌 百度

GMT+8, 2024-7-5 11:45 , Processed in 0.086774 second(s), 20 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表