No announcement yet.

Log Cleanup..

  • Filter
  • Time
  • Show
Clear All
new posts

  • Log Cleanup..

    I have searched and there was no real vbscript resolution for my issue.. I cant use Robocopy.

    Basically what I want is a script that will delete files in a folder of a specific name that are older then a certain number of days.

    This is what I have so far, and it's probably something simple but I'm new to vbscript and cant get it to work.
    Option Explicit
    	Dim MyArray(2)
    	Dim f1
    	Dim ret_time
    	Dim fso
    	Dim oFolder
    	Dim oFile
    	Dim sDirectoryPath
    	Dim index, x
      Set fso = createobject("Scripting.FileSystemObject")
      sDirectoryPath = "D:\_awcgateway\archive"
      Set oFolder = fso.GetFolder(sDirectoryPath)
      ret_time = 1
    For Each oFile In oFolder.files
           If DateDiff("d", oFile.datelastmodified,Now) > ret_time Then
                   index = 0
                   For x = 0 To UBound(MyArray)
                       If LCase(oFile.Name) = LCase(MyArray(x)) Then
                           index = 1
                           Exit For
                       End If
                   If index = 1 Then     
                    oFile.Delete True
                End if    
        End If
    Set oFolder = Nothing
    Set fso = Nothing
    So basically any help would be much appreciated!

    Edit Dumber:
    Wrapped Code tags arround the script.
    Last edited by Dumber; 6th September 2007, 11:15.

  • #2
    Re: Log Cleanup..

    Here is the log rotate script that we use here:

    Just change the line that has the folder location, and the number of days

    Option Explicit
    Dim RetentionPeriod, _
        SystemDate, SystemYear, JulianDay, SystemDateStamp, _
        ObjFileSystem, ObjFolder, ObjFileCollection, oExec, objFile, WshShell, _
        FileYear, FileJulianDay, FileDateStamp, _
        Command, DateDifference
    'Find Today's Date Information.
    RetentionPeriod = 30 'Edit this line for the number of days
    SystemDate = Date
    SystemYear = Year(SystemDate)
    JulianDay = SystemDate - DateValue("1/1/" & SystemYear) + 1
    If (JulianDay < 10) Then
      JulianDay = "00" & JulianDay
    ElseIf (JulianDay < 100) Then
      JulianDay = "0" & JulianDay
      JulianDay = JulianDay
    End If
    SystemDateStamp = SystemYear & JulianDay
    'Clean up old log files.
    Set objFileSystem = CreateObject("Scripting.FileSystemObject")
    Set objFolder = ObjFileSystem.GetFolder("C:\Program Files\EVault InfoStage\AMP Service\Logs") 'Edit this line for where the log files are located.
    Set objFileCollection = ObjFolder.Files
    For Each objFile in ObjFileCollection
      FileYear = Year(objFile.DateCreated)
      FileJulianDay = objFile.DateCreated - DateValue("1/1/" & FileYear) + 1
      If (FileJulianDay < 10) Then
        FileJulianDay = "00" & FileJulianDay
      ElseIf (FileJulianDay < 100) Then
        FileJulianDay = "0" & FileJulianDay
        FileJulianDay = FileJulianDay
      End If
      FileDateStamp = Int(FileYear & FileJulianDay)
      DateDifference = SystemDateStamp - FileDateStamp
      If (DateDifference > 365) Then
        DateDifference = DateDifference - 365
      End If
      If DateDifference > RetentionPeriod Then
      End If
    Hope that helps



    • #3
      Re: Log Cleanup..

      I have a similar situation. I have a BES server which creates SMS and Cell logs every day. For comliance reason legal requires us to keep 1 month of logs on server for vewing purposes. We could delete older logs as long as they are backed up to tape. I found the following script on
      See my post
      Attached Files


      • #4
        Re: Log Cleanup..

        Here you go Lemons... this is what I ended up with.

        Locally creates an archive folder and moves all apart from specified into that folder.

        Sorry.. I did stop doing anything with this when I hit the problem with it didn't seem to ignore spaces in foldernames - in red (I think I did come up with a solution to this - it didn't run remotely so try it... the biggest thing for me was that it needed to run remotely... which it didn't)

        I also didn't get it to enter into the log file yet... but that's not too much trouble to fix...

        ' Create folder, move all folders apart from admin
        ' Variables
        Path = "c:\folderpath" 
        ' Subfolders to be processed - True/False
        Subfolders = True
        ' Create a log file for this
        ' * Change the path to where the log file is.
        ' * Be sure the text file already exists or else 
        ' * script will fail.
        strLogFile = "c:\logs\fileArchiveLog.txt"
        'I'd recommend putting some date or something into the logfile... before it had the comptuername but since I couldn't get it to run remotely i removed that bit...
        ' Log file creation/check
        strDirectory = "c:\logs"
        strFile = "\fileArchiveLog.txt"
        ' Create the File System Object
        Set objFSO = CreateObject("Scripting.FileSystemObject")
        ' Check that the strDirectory folder exists
        If objFSO.FolderExists(strDirectory) Then
           Set objFolder = objFSO.GetFolder(strDirectory)
        Set objFolder = objFSO.CreateFolder(strDirectory)
        End If
        If objFSO.FileExists(strDirectory & strFile) Then
        Set objFolder = objFSO.GetFolder(strDirectory)
        Set objFile = objFSO.CreateTextFile(strDirectory & strFile)
        Wscript.Echo "Created " & strDirectory & strFile
        End If 
        set objFolder = nothing
        set objFile = nothing
        If err.number = vbEmpty then
        Set objShell = CreateObject("WScript.Shell")
        ' Open's Log File Directory ("Explorer" & " " & strDirectory & "\" )
        Else WScript.echo "VBScript Error: " & err.number
        End If
        ' *******************************************************
        ' Create Archive Folder and Move Folders Start
        ' *******************************************************
        ' Create Archive Folder
        dim filesys, ArchFol, ArchPath
        'ArchPath = "C:\Folderpath\_Archive_" & FormatDateTime(date(),vblongdate)
        ArchPath = "C:\Folderpath\ArchiveProf"
        set filesys=CreateObject("Scripting.FileSystemObject")
        If Not filesys.FolderExists(ArchPath) Then
        Set ArchFol = filesys.CreateFolder(ArchPath)
        End If
        ' Move folders
          Dim objFSO        : Set objFSO        = CreateObject("Scripting.FileSystemObject" )
          Dim objFolder     : Set objFolder     = objFSO.GetFolder( "c:\Folderpath" )
          Dim colSubFolders : Set colSubFolders = objFolder.Subfolders
          Dim dicSelFolders : Set dicSelFolders = CreateObject("Scripting.Dictionary" )
        dim Movesys
        set Movesys = CreateObject("Scripting.FileSystemObject")
          dicSelFolders.Add "archiveprof", ""
          dicSelFolders.Add "local"  , ""
          dicSelFolders.Add "admin"  , ""
          Dim objSubfolder
          For Each objSubfolder in colSubfolders
        '     On Error Resume Next
              If dicSelFolders.Exists( LCase( objSubfolder.Name ) ) Then
                 WScript.Echo "ignoring", objSubfolder.Path
                 WScript.Echo "moving", objSubfolder.Path
                 Movesys.MoveFolder objSubfolder.Path, "C:\Folderpath\ArchiveProf\"
              End If
        This is my signature.