Solved

Monitor mulitple folders

Posted on 2014-02-11
9
208 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 69

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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
LVL 69

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
 
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 69

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will help you understand what HashTables are and how to use them in PowerShell.
A project that enables an administrator to perform actions within a user session context not just at the time of login but any time later on day(s) or week(s) later.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

828 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