Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Monitor mulitple folders

Posted on 2014-02-11
9
Medium Priority
?
218 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 71

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
Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

 
LVL 71

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 71

Accepted Solution

by:
Qlemo earned 2000 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

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Question has a verified solution.

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

A brief introduction to what I consider to be the best editor for PowerShell.
Previously, on our Nano Server Deployment series, we've created a new nano server image and deployed it on a physical server in part 2. Now we will go through configuration.
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

670 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