I have a script that monitors a couple folders for new files/folders.
If it is a file it moves/extracts it to the appropriate spot based on name, etc and does some other post processing.
If it is a folder it waits for 45 seconds to allow the files to finish copying to the folder and then looks for archives to extract/ files to copy and moves/extracts them to the appropriate place based on name etc. and does post processing.
This all works fine.
The problem is that when say 12 folders are copied to the monitored location it takes a while to process them due to the 45 second "sleep". I would like to be able to determine if the copy operation was finished to the new folder instead of simply waiting 45 seconds if possible.
I have searched Google and this forum for options but all I find is 1000s of articles on how to copy a file.
Any help is greatly appreciated.
Here is the code with the processing/post processing code removed to make it simple.
Update: Just thought, if this is not possible another option may be to get a list of files in the directory with their sizes, wait a couple seconds and then get the list again to see if any changes. If no changes then assume copy operation is done. This may not work if the entire size of the file is allocated when the copy process is started.
Update 2: The folder in question will contain either a set of rar files or an avi file. If it is a set of rar files, the rar will be the last file copied so another option would be to test if a rar file or avi file exist. If it does not, wait 5 seconds and try again. If it does then check to see if the file is "in use" (still being copied). If it is "in use" wait 5 seconds and try again. If it is not "in use" then continue with processing.
Set objShell = CreateObject("Wscript.Shell")
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
' Monitor these folders
Set colMonitoredEvents = objWMIService.ExecNotificationQuery _
("Select * From __InstanceCreationEvent Within 10 Where " _
& "(Targetinstance ISA 'Win32_Directory' Or " _
& "TargetINstance Isa 'Cim_DataFile') And " _
& "TargetInstance.Drive = 'D:' And " _
& "(TargetInstance.Path = '\\FOLDER1\\' Or " _
& "TargetInstance.Path = '\\FOLDER2\\')")
Do
Set objLatestEvent = colMonitoredEvents.NextEvent
Set objTargetInstance = objLatestEvent.TargetInstance
If objTargetInstance.Path_.Class = "Win32_Directory" Then
' If folder created wait 45 seconds for files to copy then process folder
WScript.sleep 45000
'Process folder and files post processing code
Else
' Else process file and post processing code
End If
Loop
Using the idea in "Update 2" the following code works which may or may not help others in the future
Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")
Set folder = fso.getfolder(strDirName)
Do
For Each File in Folder.Files
If fso.GetExtensionName(File)="rar" OR _
fso.GetExtensionName(File)="avi" Then
On Error Resume Next
Set objTextFile = fso.OpenTextFile(file, 8, True)
If Err.Number = 0 Then
objTextFile.Close
Exit Do
End If
End If
Next
WScript.sleep 5000
Loop
<message edited by mikesmithnova on Monday, December 05, 2011 9:09 PM>