Solved

Monitor mulitple folders

Posted on 2014-02-11
9
197 Views
Last Modified: 2014-06-12
Okay so seems that my original questions was not something practical. I am very new to vbscript so I guess I just don't understand the complexity I asked. so I will ask another question to see if anyone can help me out.

Monitor 3 different folders every 10 seconds and copy to 3 different folders.

Folder 1 copies to location 1
folder 2 copies to location 2
folder 3 copies to location 3
Below is still what I got.

If my mistake is that it is not possible to do with 1 script and I just need to run a different script for each folder, I understand. Or if I am just flat out going about this the wrong way, please let me know, some guidance would be greatly appreciated.


''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'Run on local computer
strComputer = "."
''''''''''''''''''''''''''''''''''''''''''''''''''''''''


CONST strDestFolder="E:\public\salesname1\contractfolder1\"

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set colMonitoredEvents = objWMIService.ExecNotificationQuery("SELECT * FROM __InstanceCreationEvent WITHIN 10 WHERE " & "TargetInstance ISA 'CIM_DirectoryContainsFile' and " & "TargetInstance.GroupComponent= " & "'Win32_Directory.Name=""C:\\\\IT""'")

Set objFiles = CreateObject("Scripting.FileSystemObject")


Do While True
    Set objEventObject = colMonitoredEvents.NextEvent()
    strNewFile = objEventObject.TargetInstance.PartComponent
    arrNewFile = Split(strNewFile, "=")
    strNewFileName = arrNewFile(1)
    strNewFileName = Replace(strNewFileName, "\\", "\")
    strNewFileName = Replace(strNewFileName, Chr(34), "")
     
    objFiles.CopyFile strNewFileName, strDestFolder, True

Loop

Open in new window

0
Comment
Question by:Barron1299
  • 4
  • 3
9 Comments
 
LVL 8

Expert Comment

by:TheNautican
ID: 39863641
Here's something real simple to try.

Regards,
-Naut

Dim dteWait
Dim dteRun

Set objFolder = CreateObject("Scripting.FileSystemObject")

dteRun = DateAdd("h", 8, Now()) 'run for 8 hours
dteWait = DateAdd("s", 10, Now()) 'check every 10 seconds

Do Until (Now() > dtwRun) 'Main loop
	Do Until (Now() > dteWait) 'inner loop
	Loop

	objFolder.CopyFolder "C:\folder1", "E:\public\salesname1\"
	objFolder.CopyFolder "C:\folder2", "E:\public\salesname2\"
	objFolder.CopyFolder "C:\folder3", "E:\public\salesname3\"
Loop

WScript.Echo "Done"

Open in new window

0
 
LVL 68

Expert Comment

by:Qlemo
ID: 39865663
The inner loop should just being a spinner - that adds enormous CPU stress, without any need. Why not using a sleep here? Anyway, I cannot recommend such a polling script, it negates anything you want to achieve by using a filesystemwatcher object.

Indeed, and AFAIK, you cannot set a filesystemwatcher to more than one object. But you can set up multiple FSWs! But since VBS does not have all the features required to make it smooth (or possible) to handle multiple event handlers, the most simple option is to have one script running per folder to monitor.

Using PowerShell allows to implement a single script, though.
0
 
LVL 1

Author Comment

by:Barron1299
ID: 39868745
sorry for the late response, I will give the script provided a try see how my system can handle it.

Qlemo: I am not sure I understand when you say spinner, that is way over my head. but what I believe you are saying to me is either using 1 script per folder I want to monitor or write it in Powershell?
0
 
LVL 68

Expert Comment

by:Qlemo
ID: 39868783
"Spinner" is a part of code going thru a loop, waiting for nothing else than an event. It is called that because it "rotates" very fast. If one codes a wait loop, the body should at least contain a sleep statement, to allow other code to run and lessen the CPU stress. Else the CPU is tending to do nothing else than to run the loop.

And yes, you got it right. That are my suggestions.
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 1

Author Comment

by:Barron1299
ID: 39868811
Okay TheNautican, no disrespect but since I know once this works the amount of folders I need to monitor are going to increase, I am going to attempt to write it in Powershell.

I have no idea how to write it in Powershell, but I don't really know how to code in VBScript either so guess not a big deal. hahaha.
0
 
LVL 68

Accepted Solution

by:
Qlemo earned 500 total points
ID: 39868894
foreach ($folders in ('C:\SourceFolder1', 'D:\TargetFolder1'),
                     ('C:\SourceFolder2', 'D:\TargetFolder2'),
                     ('C:\SourceFolder3', 'D:\TargetFolder3'))
{
  Register-ObjectEvent (New-Object IO.FileSystemWatcher $folders[0] -Property @{ NotifyFilter = [IO.NotifyFilters] 'FileName, LastWrite' }) -Action {
    copy-item $Event.SourceEventArgs.FullPath $folders[1]
  }
}

while ($true) { sleep 10 }

Open in new window

This will run until you stop it :D, As your initial script, actions will take place every 10 seconds.
0
 
LVL 1

Author Comment

by:Barron1299
ID: 39870929
Thank you, I am trying to implement it today, let you know how it works out.
0
 
LVL 1

Author Closing Comment

by:Barron1299
ID: 40131225
Sorry I totally thought I had accepted this already. thank you all for our help.
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Create and license users in Office 365 in bulk based on a CSV file. A step-by-step guide with PowerShell script examples.
This article will help you understand what HashTables are and how to use them in PowerShell.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now