3

I wish to automate some tasks. I need to move files from one folder to another. I have written the script below which is working fine.

But my requirement is to trigger this VB script automatically, in the background, when a file is created or added to a folder.

'Script Begins

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

'Script Ends
Smandoli
  • 6,919
  • 3
  • 49
  • 83
vicki
  • 157
  • 2
  • 23
  • The usual approach, assuming Windows, is to use Task Scheduler to run the script every few minutes. The script detects a file, performs action, and if necessary records to a log (for example to avoid processing the same file again in the future). – Smandoli Apr 09 '16 at 14:47

1 Answers1

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
  • Thanks for the replies. I am not an expert at VBS. Please can you make appropriate changes to my given code above. – vicki Apr 09 '16 at 23:59
  • Thanks again. I do not wish to use Task Scheduler. The above "InstanceCreationEvent" is smart. But how do mate this creation event with my code above ? – vicki Apr 11 '16 at 12:05
  • I edited one of them. You are required to be a programmer (or wannabe one). –  Apr 11 '16 at 12:39
  • I am quite a slow wannabe programmer. Excuse me on this. I have been racking my brains, but haven't got an answer. please could you help and mate the two. Would really appreciate the help. – vicki Apr 12 '16 at 16:04
  • I edited the first sample code. –  Apr 12 '16 at 21:58