'------ Vovin Services-----

'Cleans out error files generated by Commence upon sync failure
'Checks for .err and .ser files used by currently active Commence database
'Checks for foldernames starting with "slx" in system temp folder
'If any are found, they are deleted
'Requires Commence 4.1 or above
'Author: Arno van Boven
'Created: 30-10-01

'----------------------------------------------------------

Option Explicit

Const TemporaryFolder = 2
Const cErrorPath = "slx" 'temp dirs created by Commence start with 'slx'
Dim dbname 'database name, used only to inform user
Dim arrErrorFile 'array of error file wildcards
Dim workgrp 'path to look in
Dim temppath 'system %temp% directory
Dim cmc 'Commence
Dim s, i, fso, fld, sfld, fl
Dim filespec, filecount, fldcount
Dim Reply

If WScript.Version < 3 Then
	MsgBox "VBscript 3.0 or above is required", vbCritical, "Error"
	WScript.Quit
End If

'initialize some variables
arrErrorFile = Array("*.err","*.ser")
filecount = 0
fldcount = 0

'talk to Commence
Set cmc = CreateObject("Commence.DB")
dbname = cmc.Name
workgrp = cmc.Path & "\workgrp" 'obtain pathname where sync files are collected in the process
Set cmc = Nothing

'tell user what we are about to do
Reply = MsgBox("Currently active database is " & dbname _
			& vbCrLf & vbCrLf _
			& "This script will try to delete any .err and .ser files" _
			& vbCrLf & "that Commence may have created."_
			& vbCrLf & "It also checks for any temporary folders that " _
			& vbCrLf & "Commence may have failed to delete."_
			& vbCrLf & vbCrLf _
			& "If it finds any of these files or folders," _
			& vbCrLf & "they will be automatically deleted."_
			& vbCrLf & "This will not harm your system."_
			& vbCrLf & vbCrLf _
			& "Continue?", vbQuestion + vbOkCancel, "SyncClean")
If Reply <> vbOK Then  WScript.Quit

'we are interested in files and folders		
Set fso = CreateObject("Scripting.FileSystemObject")
Set fld = fso.GetFolder(workgrp)

'check for any error files in WORKGRP folder
'if found, delete them
For each fl in fld.Files
	s = fl.Name
	If (Right(s,4) = Right(arrErrorFile(0),4)) Or (Right(s,4) = Right(arrErrorFile(1),4)) Then
		filecount = filecount + 1
		fl.Delete
	End If
Next

'clean out %TMP% folder; look for folders starting with slx* and delete them
temppath = fso.GetSpecialFolder(TemporaryFolder)
Set fld = fso.GetFolder(temppath)
For each sfld in fld.SubFolders
	s = sfld.Name
	'only delete them if they havent been accessed in the last minute
	'1 minute is a very arbitrary value, choosing a relatively large value
	'ensures you that you are not trying to delete a folder that is currently in use
	If Left(s,3) = cErrorPath Then
		If sfld.DateLastAccessed < DateAdd("n", -1, Now) Then
			fldcount = fldcount + 1
			sfld.Delete
			MsgBox s
		End If
	End If
Next

'display information to user
If (filecount > 0) Or (fldcount > 0) Then
	MsgBox "Done. " & filecount & " Files and " & fldcount & " folders were deleted", vbExclamation, "Errors found"
Else
	MsgBox "Done. No errors were found", vbInformation, "No errors"
End If

'release objects
Set cmc = Nothing
Set fld = Nothing
Set fso = Nothing

WScript.Quit