'
'  FileReaper.lss
'
'  Time-stamp: <2002-02-02 15:43:26 deriksso>
'
'  Date        Author           Changes
'  ----------  ---------------  ------------------------------------
'  2000-08-25  Daniel Eriksson  Created
'

Public Class FileReaper
' This is a class for cleaning up after applications that create
' temporary files. It deletes files based on file masks.
'
'
' Example:
' Dim reaper As FileReaper
' Set reaper = New FileReaper("c:\temp\*.tmp")
' Call reaper.reap()
'
' You need to include these files:
' %INCLUDE "LSCONST.LSS"
' %INCLUDE "lserr.lss"

Private fileMask_ As String

Sub New(fileMask As String)
  fileMask_ = fileMask
End Sub

Private Function stringLeftBack(src As String, substr As String) As String
  'TODO: Shape up this function.
  If Rightbp$(src, Len(substr)) = substr Then
    stringLeftBack = Leftbp$(src, Len(src) - Len(substr))
  Else
    stringLeftBack = stringLeftBack(Leftbp$(src, Len(src) - Len(substr)), substr)
  End If
End Function

Private Function getDirectory(path As String) As String
'Returns directory part of a file path
'
  Dim separator As String
  Dim positionOfChar As Long
  separator$ = "\"
  getDirectory = stringLeftback(path, separator)
  'getDirectory = Strleftback(path, separator)
End Function

Public Sub setPath(path As String)
'Accessor method
  fileMask_ = path
End Sub

Public Sub reap
'Deletes files. If a file is not found we just continue.
  On Error 75 Resume Next 'ErrPathFileAccessError
  Dim directory As String
  directory = getDirectory(fileMask_)
  Dim fileName As String
  fileName$ = Dir$(fileMask_, 0) 'ATTR_NORMAL
  Do While fileName$ <> ""
    Kill directory & "\" & fileName$
    fileName$ = Dir$()
  Loop
End Sub
End Class