Solved

Monitor mulitple folders

Posted on 2014-02-11
9
215 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 70

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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
LVL 70

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 70

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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.
A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
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…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

624 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