如何通过vbs监控文件夹文件 [英] How to monitoring folder files by vbs

查看:51
本文介绍了如何通过vbs监控文件夹文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能帮我做错地方?此脚本用于监视文件夹中的创建、删除或修改文本文件

Can anyone help me where i do mistake ? this script is for monitoring folder for create, delete or modified text files

sPath = "C:\scripts\test"
sComputer = "."
sDrive = split(sPath,":")(0)
sFolders1 = split(sPath,":")(1)
sFolders = REPLACE(sFolders1, "\", "\\") & "\\"

Set objWMIService = GetObject("winmgmts:\\" & sComputer & "\root\cimv2")
Set colMonitoredEvents = objWMIService.ExecNotificationQuery _
("SELECT * FROM __InstanceOperationEvent WITHIN 1 WHERE " _
& "TargetInstance ISA 'CIM_DataFile' AND " _
& "TargetInstance.Drive='" & sDrive & "' AND " _
& "TargetInstance.Path='" & sFolders & "' AND " _
& "TargetInstance.Extension = 'txt' ")

Wscript.Echo vbCrlf & Now & vbTab & _
"Begin Monitoring for a Folder " & sDrive & ":" & sFolders1 & " Change Event..." & vbCrlf

Do
    Set objLatestEvent = colMonitoredEvents.NextEvent
    Select Case objLatestEvent.Path_.Class

    Case "__InstanceCreationEvent"
    WScript.Echo Now & vbTab & objLatestEvent.TargetInstance.FileName & "." & objLatestEvent.TargetInstance.Extension _
    & " was created" & vbCrlf

    Case "__InstanceDeletionEvent"
    WScript.Echo Now & vbTab & objLatestEvent.TargetInstance.FileName & "." & objLatestEvent.TargetInstance.Extension _
    & " was deleted" & vbCrlf

    Case "__InstanceModificationEvent"
    If objLatestEvent.TargetInstance.LastModified <> _
    objLatestEvent.PreviousInstance.LastModified then
    WScript.Echo Now & vbTab & objLatestEvent.TargetInstance.FileName & "." & objLatestEvent.TargetInstance.Extension _
    & " was modified" & vbCrlf
    End If
    End Select
Loop

Set objWMIService = nothing
Set colMonitoredEvents = nothing
Set objLatestEvent = nothing

这个脚本在我写的时候完美运行

This script is run perfect when i write

sPath = "\\ComputerName\C$\scripts\test"

插入

sPath = "C:\scripts\test"

谢谢....

推荐答案

如果您在 google 上搜索 "WMI TargetInstance.Drive",您会发现驱动器号需要一个冒号.像

If you google for "WMI TargetInstance.Drive", you'll see that the drive letter needs a colon. A query like

SELECT * FROM __InstanceOperationEvent WITHIN 1 WHERE TargetInstance ISA 'CIM_DataFile' AND TargetInstance.Drive='E:' AND TargetInstance.Path='\\trials\\SoTrials\\answers\\10041057\\data\\' AND TargetInstance.Extension = 'txt'

按预期工作.

这篇关于如何通过vbs监控文件夹文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆