Hello, I have a script here that I am using (with the help of the great people here on Tek-Tips). What I have now works good for me, but what I would like to do is watch the file for change, then copy the file out, just like I am doing in this script. The problem is, I want to copy the file and save a different copy of the file each time it is saved. How can I do this?
I will use this for an Excel file actually. I then want to create maybe 10 copies of the file in the directory and then purge out the older ones once they reach greater than 10. Any simple ideas?
Thanks for your help, as always.
I will use this for an Excel file actually. I then want to create maybe 10 copies of the file in the directory and then purge out the older ones once they reach greater than 10. Any simple ideas?
Thanks for your help, as always.
Code:
dim filesys, objfolder, strdir
set filesys=CreateObject("Scripting.FileSystemObject")
strdir = "C:\temp\test\"
'If filesys.FolderExists(strdir) Then
'filesys.CopyFile "C:\test\test.txt", strdir, True
strComputer = "."
strDrive = "C:"
strFolder = "\\temp\\"
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colProcess = objWMIService.ExecQuery("Select * from Win32_Process where name = 'wscript.exe' or name = 'cscript.exe'")
If colProcess.Count > 1 Then
' --- a wscript or cscript process is already running
WScript.quit
End If
' Create the event sink object that receives the events
Set objSink = wscript.CreateObject("WbemScripting.SWbemSink", "SINK_")
strComputer = "."
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
objWMIService.ExecNotificationQueryAsync objSink, "SELECT * FROM __InstanceOperationEvent WITHIN 3 " & _
"Where Targetinstance Isa 'CIM_DataFile' And " & _
"TargetInstance.Drive = '" & strDrive & "' And " & _
"TargetInstance.Path = '" & strFolder & "'"
Done = False
Do Until Done ' We'll end after first event for this example
wscript.sleep 1000
Loop
filesys.CopyFile "C:\temp\test.txt", strdir, True 'copy file
Msgbox "All Done"
Sub SINK_OnObjectReady(wmiObject, wmiAsyncContext)
wscript.echo wmiObject.Path_.Class ' what event was it?
wscript.echo wmiObject.TargetInstance.Name
Done = True
End Sub