The following Visual Basic Script allows you to monitor a folder for new files. The script will try to process the file if the file is still locked by the application that created the file, an error will occur and the script will handle it by waiting a growing amount of time and then retrying to use the file.
The script will also send an email alert about the error raised:
strComputer = "." strPathToWatch = "C:\backups\" Sub SendMailMessage(to_address, subject, msg) dim sch, cdoConfig, cdoMessage sch = "http://schemas.microsoft.com/cdo/configuration/" Set cdoConfig = CreateObject("CDO.Configuration") With cdoConfig.Fields .Item(sch & "sendusing") = 2 ' cdoSendUsingPort .Item(sch & "smtpserver") = "yoursmtpserver" .update End With Set cdoMessage = CreateObject("CDO.Message") With cdoMessage Set .Configuration = cdoConfig .From = "script-error@yourcompany.com" .To = to_address .Subject = subject .TextBody = msg .Send End With Set cdoMessage = Nothing Set cdoConfig = Nothing End Sub 'Build the queries that will monitor for new files Set objWMIService = GetObject("winmgmts:" _ & "{impersonationLevel=impersonate}!\\" & _ strComputer & "\root\cimv2") 'Change the following query when you want to watch a folder different from D:\\\\ Set colMonitoredEvents = objWMIService.ExecNotificationQuery _ ("SELECT * FROM __InstanceCreationEvent WITHIN 10 WHERE " _ & "Targetinstance ISA 'CIM_DirectoryContainsFile' and " _ & "TargetInstance.GroupComponent= " _ & "'Win32_Directory.Name=""C:\\\\backups\\\\""'") 'Keep getting the next event. Each event represents the creation of a new file in the specified folder Do Set objLatestEvent = colMonitoredEvents.NextEvent strNewFile = objLatestEvent.TargetInstance.PartComponent arrNewFile = Split(strNewFile, "=") strFileName = arrNewFile(1) strFileName = Replace(strFileName, "\\", "\") strFileName = Replace(strFileName, Chr(34), "") 'Gets the full path of the file created Dim fso Set fso = CreateObject("Scripting.FileSystemObject") file_needs_processing = true time_to_wait = 40000 'wait 40000 milisenconds (40 seconds) before retrying Do While file_needs_processing = true On Error Resume Next Err.Clear 'Do processing here if Err.Number = 0 then 'File was succesfully processed, disable the error handling, rename and delete the file On Error GOTO 0 'Stop trying to process this file, we are done file_needs_processing = false else 'An error occurred, send an email with the error message, make the script wait a period of time and then try again to process the file err_msg = "Error # " & CStr(Err.Number) & " " & Err.Description & " File: " & strFileName On Error GOTO 0 SendMailMessage "you@yourcompany.com" , "File Failed, going to re-try in " & Cstr(time_to_wait / 1000) & " seconds", err_msg WScript.Echo err_msg WScript.Echo "Waiting for file to be ready... going to re-try in " & Cstr(time_to_wait / 1000) & " seconds" WScript.Sleep time_to_wait time_to_wait = time_to_wait + 30000 'add 30 seconds to wait period end if loop Loop
Comments