Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Monitor mulitple folders

Posted on 2014-02-11
9
Medium Priority
?
219 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 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
WatchGuard Case Study: NCR

With business operations for thousands of customers largely depending on the internal systems they support, NCR can’t afford to waste time or money on security products that are anything less than exceptional. That’s why they chose WatchGuard.

 
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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
A walk-through example of how to obtain and apply new DID phone numbers to your cloud PBX enabled users that are configured in Office 365. Whether you have 1, 10 or 100+ users in your tenant, it's quite easy to get them phone-enabled and making/rece…
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 anti-spam), the admin…
Screencast - Getting to Know the Pipeline

963 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