vbScript to Rotate Log

Many applications write log file.  Unfortunately, these applications have no provision of log rotation i.e. change the log file name daily.  Therefore, the log file is very big at the end which is very difficult to handle.  In many case, the log files use up all the disk space.


The below vbscript will rotate the log by rename the log file into a *YYYYMMDD.csv format, so that the each file will store only one day of data.

You may use another vbscript file written by me to delete old log file (e.g. log file older than 6 months) in order to save disk space.




Set fso = CreateObject("Scripting.FileSystemObject")

strDate = JXIsoDate(date)

DirWalk "D:\Logs\"

Sub DirWalk(parmPath)
Dim oSubDir, oSubFolder, oFile, n

On Error Resume Next         ' We'll handle any errors ourself, thank you very much

   Set oSubFolder = fso.getfolder(parmPath)

   For Each oFile In oSubFolder.Files   ' look in the current dir
      If Err.Number <> 0 Then   ' if we got an error, just skip this entry
'msgbox "Error"
          Err.Clear
      'ElseIf oFile.DateLastModified < Date() - parmDays Then
      ElseIf right(oFile.Path, 3) = "log" Then    The filter for the files
         fso.MoveFile  oFile.Path, (left(oFile.Path, len(oFile.Path)-4) &amp; strDate &amp; ".csv")
      End If
   Next

   On Error Goto 0              ' Resume letting system handle errors.

End Sub

Function JXIsoDate(dteDate)
'Version 1.0
   If IsDate(dteDate) = True Then
      DIM dteDay, dteMonth, dteYear
      dteDay = Day(dteDate)
      dteMonth = Month(dteDate)
      dteYear   = Year(dteDate)
      JXIsoDate = dteYear &amp; _
          Right(Cstr(dteMonth + 100),2) &amp; _
          Right(Cstr(dteDay + 100),2)
   Else
      JXIsoDate = Null
   End If
End Function

Comments

Popular Posts