[Solved Sorta] Help with detecting Copy operation in progress/completed

Author Message
mikesmithnova

  • Total Posts : 2
  • Scores: 0
  • Reward points : 0
  • Joined: 12/5/2011
  • Status: offline
[Solved Sorta] Help with detecting Copy operation in progress/completed Monday, December 05, 2011 5:14 PM (permalink)
0

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>
 
#1
    mikesmithnova

    • Total Posts : 2
    • Scores: 0
    • Reward points : 0
    • Joined: 12/5/2011
    • Status: offline
    Re:[Solved Sorta] Help with detecting Copy operation in progress/completed Monday, December 05, 2011 9:41 PM (permalink)
    0
    Wouldn't let me edit original post again but a little note.
     
    Note: I suppose this could also be easily adapted for all files by trying to open each file in the folder and if any give an error sleep for 5 seconds. This may not be 100% if the check happens just after a file finishes and before a new one begins though. A workaround to that may be running the check twice as the chance of the timing being that exact twice is unlikely.
     
    #2

      Online Bookmarks Sharing: Share/Bookmark

      Jump to:

      Current active users

      There are 0 members and 1 guests.

      Icon Legend and Permission

      • New Messages
      • No New Messages
      • Hot Topic w/ New Messages
      • Hot Topic w/o New Messages
      • Locked w/ New Messages
      • Locked w/o New Messages
      • Read Message
      • Post New Thread
      • Reply to message
      • Post New Poll
      • Submit Vote
      • Post reward post
      • Delete my own posts
      • Delete my own threads
      • Rate post

      2000-2012 ASPPlayground.NET Forum Version 3.9