将文件添加到文件夹时触发VBScript

时间:2016-04-09 14:04:21

标签: file vbscript directory

我希望自动执行某些任务。我需要将文件从一个文件夹移动到另一个文件夹。我写了下面的脚本,工作正常。

但我的要求是在创建文件或将文件添加到文件夹时,在后台自动触发此VB脚本。

'Script Begins

With CreateObject("Scripting.FileSystemObject")
    .MoveFile "C:\Source\Files\*.*", "D:\Destination\Files\"
End With

'Script Ends

1 个答案:

答案 0 :(得分:0)

InstanceCreationEvent

Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
Set colMonitoredEvents = objWMIService.ExecNotificationQuery _
("SELECT * FROM __InstanceCreationEvent WITHIN 10 WHERE Targetinstance ISA 'CIM_DirectoryContainsFile' and TargetInstance.GroupComponent= 'Win32_Directory.Name=""c:\\\\scripts""'")
Do
    Set objLatestEvent = colMonitoredEvents.NextEvent
    With CreateObject("Scripting.FileSystemObject")
        .MoveFile "C:\Source\Files\*.*", "D:\Destination\Files\"
    End With
Loop

InstanceModificationEvent

Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
Set colMonitoredEvents = objWMIService.ExecNotificationQuery _
("SELECT * FROM __InstanceModificationEvent WITHIN 10 WHERE Targetinstance ISA 'CIM_DirectoryContainsFile' and TargetInstance.GroupComponent= 'Win32_Directory.Name=""c:\\\\scripts""'")
Do
    Set objLatestEvent = colMonitoredEvents.NextEvent
    Wscript.Echo objLatestEvent.TargetInstance.PartComponent
Loop

InstanceDeletionEvent

Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
Set colMonitoredEvents = objWMIService.ExecNotificationQuery _
("SELECT * FROM __InstanceDeletionEvent WITHIN 10 WHERE Targetinstance ISA 'CIM_DirectoryContainsFile' and TargetInstance.GroupComponent= 'Win32_Directory.Name=""c:\\\\scripts""'")
Do
    Set objLatestEvent = colMonitoredEvents.NextEvent
    Wscript.Echo objLatestEvent.TargetInstance.PartComponent
Loop