<?XML version="1.0" standalone="yes" ?>
<!--------------------------------------------------------------------------

    Project: Tools

    Module:  DeleteOldTraces.wsf

    Description: script to delete old trace files
    
    Comments:
    
 --------------------------------------------------------------------------
     $Date:: 10.08.05 12:10                                              $
  $Modtime:: 10.08.05 11:58                                              $
 $Revision:: 2                                                           $
 $Workfile:: DeleteOldTraces.wsf                                         $
 -------------------------------------------------------------------------->

<job>
    <runtime>
        <description>This script deletes old trace files.</description>
        <example>Example: DeleteOldTraces.wsf /path:"D:\Traces\" /age:"1"</example>
    
        <named  name = "path"
                helpstring = "system path to logfiles, default is taken from registry"
                type = "string"
                required = "false"
        />
        <named  name = "age"
                helpstring = "age of files in days, which will be deleted, default is 3 days, accepted values have to be > 1"
                type = "string"
                required = "false"                
        />
    </runtime>
    
    <script language="VBScript">
    <![CDATA[

    Option Explicit
        
    Dim objArgsNamed                                         'define input parameters
    Set objArgsNamed = Wscript.Arguments.Named
               
    Dim strManufacturer, strDelPath, strFileType, strDelFiles, iMaxFileAge, WSHShell     ' define script parameters
    strManufacturer = "SWYX"                                 ' change this variable for OEMs, to get info from registry
    strFileType = "*.log"                                    ' definition of file type 
    iMaxFileAge = 3                                          ' all files older than (default 5) days will be deleted
    
    
    'main ---------------------------------------------------------------------------------------------------------------------------------------------
    if (objArgsNamed.Exists("path")) Then                             ' test for user defined log-path from command line
        strDelPath = objArgsNamed.Item("path")                        ' set to user defined
    else
        strDelPath = GetDelString(strManufacturer)                    ' get path from registry
    End If
    
    if (objArgsNamed.Exists("age")) Then                              ' test for user defined age of logfiles
    	if not( isNumeric( objArgsNamed.Item("age") ) ) Then                          ' test for numeric parameter
    	    set WSHShell = CreateObject( "WScript.Shell" )
            WSHShell.LogEvent 2, "Invalid parameter value: age=" & objArgsNamed.Item("age")
            Wscript.quit (1)
        End If
        iMaxFileAge = cInt( objArgsNamed.Item("age") )                ' set user defined and cast to int
        if( iMaxFileAge < 1) Then                                     ' only > 1 is accepted
            set WSHShell = CreateObject( "WScript.Shell" )
            WSHShell.LogEvent 2, "Invalid parameter value: age=" & objArgsNamed.Item("age")
            Wscript.quit (1)
        End If
    End If
    
    strDelFiles = DelLogFiles(strDelPath, strFileType, iMaxFileAge)   ' execute deletion of old logfiles
    
    
    ' Functions ---------------------------------------------------------------------------------------------------------------------------------------

    ' sub: GetDelString, parameters: strManufacturer, return value: strDelPath
    ' description: this function reads a vendor specific registry key from server log-path, to get the deletion path
    Private Function GetDelString(ByVal strManufacturer)
	    Dim WSHShell, strRegPath, strRegKeyValue, chDelimiter, strLogPath, strFileType, strDelPath
    
        strRegPath = "HKLM\SOFTWARE\" & strManufacturer & "\IpPbxSrv\CurrentVersion\Tracing\logfile"
    
        Set WSHShell = CreateObject( "WScript.Shell" )
        strRegKeyValue = WSHShell.RegRead( strRegPath )                           ' read reg-key via Wscript-object
        'Wscript.echo "Registry entry: " & strRegPath & " = " & strRegKeyValue
        chDelimiter = inStrRev( strRegKeyValue, "\", "-1", 1 )                    ' search for last "\" to get path
        strLogPath = Mid( strRegKeyValue, 1, chDelimiter )
        'Wscript.echo "Delete old trace-files: " & strLogPath & strFileType
        strDelPath = strLogPath & strFileType
        GetDelString = strDelPath                                                 ' return path
    End Function

    ' sub: DelLogFiles, parameters: strPath, strType, iMaxAge, return value: strEvent
    ' description: this function tests each file in given directory for deletion.
    '           the conditions for deletion are defined by function parameters
    Private Function DelLogFiles(ByVal strPath, ByVal strType, ByVal iMaxAge)
    
        On Error Resume Next    
    
        Dim fso, folder, filelist, file, strEvent, arDelFiles(), iCount, I, iErrCount, WSHShell
    
        iCount = 0                                              ' index counter
        Set fso = CreateObject("Scripting.FileSystemObject")    ' fso, from scripting runtime
        Set folder = fso.GetFolder(strPath)                     ' get deletion folder
        if (err.number = 76) Then				' test for existing path (76: path not found)
            set WSHShell = CreateObject( "WScript.Shell" )
            WSHShell.LogEvent 2, "Invalid parameter value: path=" & objArgsNamed.Item("path") & ", error: " & err.description
            Wscript.quit (1)
        End If
        set filelist = folder.Files                             ' get all files
    
        ReDim arDelFiles(filelist.count)                        ' get file count and define Array in appropriate size
    
        For Each file in filelist                               ' test each file
            if ( DateDiff("d", file.DateLastModified, Now) > iMaxAge ) Then            ' date test
                'Wscript.echo "Filename: " & file.name & " " & file.DateCreated & " " & file.DateLastAccessed & " " & file.DateLastModified
                'wscript.echo "file is " & DateDiff("d", file.DateLastModified, Now) & " days old"
                arDelFiles(iCount) = file.path                  ' save path and name
                iCount = (iCount + 1)                           ' count files for deletion
            End If
        Next
    
        Set folder = Nothing                                    ' release folder object
        
        For I = 0 To (iCount - 1)                               ' delete files in array
            fso.DeleteFile(arDelFiles(I))
            If (Err <> 0) Then                                  ' check for errors
                if (err.number = 70) Then                              ' err 70: permission denied
                    iErrCount = (iErrCount + 1)                 ' test for failure of all deletions, therefore count all 70s
                    'Wscript.echo "permission error: " & arDelFiles(I)
                else                                            ' other error occured
                    'Wscript.echo "Filename: " & arDelFiles(I) & " Err: " & Err & ": " & Err.Description
                end if
            else
                strEvent = strEvent & arDelFiles(I) & ", "      ' success, save name for eventlog
                'Wscript.echo "deleted: " & arDelFiles(I)
            End If
            Err.Clear                                           ' clear error variable
        Next
        
        set WSHShell = CreateObject( "WScript.Shell" )
        
        if (iErrCount = iCount) Then
            ' create eventlog entry: WARNING
            WSHShell.LogEvent 2, "Deleting old SwyxWare trace files failed: permission denied!" & vbCrLf &_
                                 "Propably all the rest of trace files is locked by running services ..."
        else
            ' create eventlog entry: INFO, other errors are ignored
            WSHShell.LogEvent 4, "The following SwyxWare trace files were deleted: " & strEvent
        End If
        
        DelLogFiles = strEvent                                    ' return list of deleted files
    end Function

    ]]>     
    </script>        
</job>

<!-- EOF -->