Announcement

Collapse
No announcement yet.

Script for 1 month retention

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Script for 1 month retention

    I have a BES server which creates daily log files for SMS, phone, pin, etc. These logs average around 150 mb per day. For compliance reason we need to have 1 month of log files Accessible by legal dept and 3 years of tape backup. What i have done is shared the drive on the BES server for legal dept to view and added these logs to my nightly backup. I have been manually deleting the log files older than one month but what i would like to do is create a script which will run everyday via scheduled task and delete log files older than one month old.

  • #2
    Re: Script for 1 month retention

    If the old files are all in the same folder, then you can use a vbscript something like this:

    Code:
    'target
    strFolder = "\\computer\share\folder"
    
    'filter:
    FilesOlderThen = 30 '(days)
    
    
    'Constants returned by File.Attributes
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    Const FileAttrNormal     =    0
    Const FileAttrReadOnly   =    1
    Const FileAttrHidden     =    2
    Const FileAttrSystem     =    4
    Const FileAttrVolume     =    8
    Const FileAttrDirectory  =   16
    Const FileAttrArchive    =   32 
    Const FileAttrAlias      = 1024
    Const FileAttrCompressed = 2048
    
    'create the objects and collections for this script:
    Set fso       = CreateObject("Scripting.FileSystemObject")
    Set objFolder = fso.GetFolder(strFolder)
    Set colFiles  = objFolder.Files
    
    call popupBox(strFolder & " - " & colFiles.count & " files" ,2)
    
    'calculating date:
    ddate = DateAdd("d", "-"&FilesOlderThen, date())
    
    'start examine each file in the folder:
     For each file In colFiles
    
        If file.DateLastModified < ddate Then
    
          'Gathering information (optional) ''''''''''''''''''''''''''''''''''''''''''''''
          sFile = "File """ & file.Name & """ is older then " &  FilesOlderThen & " days:"
          sFile = sFile & vbNewLine & vbNewLine
          sFile = sFile & "File Name"  & vbTab & vbTab & ": " & file.Name
          sFile = sFile & vbNewLine
          sFile = sFile & "Name (short)"       & vbTab & ": " & file.ShortName
          sFile = sFile & vbNewLine
          sFile = sFile & "Date Created"       & vbTab & ": " & file.DateCreated
          sFile = sFile & vbNewLine
          sFile = sFile & "Date Last Modified" & vbTab & ": " & file.DateLastModified 
          sFile = sFile & vbNewLine   
          sFile = sFile & "Date Last Accessed" & vbTab & ": " & file.DateLastAccessed
          sFile = sFile & vbNewLine
          sFile = sFile & "Type"       & vbTab & vbTab & ": " & file.Type
          sFile = sFile & vbNewLine
          sFile = sFile & "Size"       & vbTab & vbTab & ": " & file.Size & " bytes."
          sFile = sFile & vbNewLine
          sFile = sFile & "Path"       & vbTab & vbTab & ": " & file.Path
          sFile = sFile & vbNewLine
          sFile = sFile & "Path (short)"       & vbTab & ": " & file.ShortPath
          sFile = sFile & vbNewLine
          sFile = sFile & "Attributes"         & vbTab & ": " & ShowAllAttr(File.Attributes)
          sFile = sFile & vbNewLine
          sFile = sFile & "Parent Folder"      & vbTab & ": " & file.ParentFolder
          sFile = sFile & vbNewLine
          sFile = sFile & "Drive"      & vbTab & vbTab & ": " & file.Drive
          call popupBox(sFile, 5) ''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    
          'Delete the file
       '   fso.DeleteFile file.Path, True    '  Uncomment this line to delete the files
    
        End If
    
     Next 'file
     
    Wscript.quit
    
     
    Function ShowAllAttr(Attr)
       If Attr = 0 Then
          ShowAllAttr = "Normal"
          Exit Function
       End If
       If Attr And FileAttrDirectory Then S = S & "Directory "
       If Attr And FileAttrReadOnly Then S = S & "Read-Only "
       If Attr And FileAttrHidden Then S = S & "Hidden "
       If Attr And FileAttrSystem Then S = S & "System "
       If Attr And FileAttrVolume Then S = S & "Volume "
       If Attr And FileAttrArchive Then S = S & "Archive "
       If Attr And FileAttrAlias Then S = S & "Alias "
       If Attr And FileAttrCompressed Then S = S & "Compressed "
       ShowAllAttr = S
    End Function
    
    sub popupBox(message, seconds)
      With CreateObject("Wscript.Shell")
       .popup (message),seconds
       .Run chr(34),0,false '<-- just to keep focus
      End With
    End Sub
    (batch version: http://forums.petri.com/showthread.p...5962#post75962)

    The vbscript above contains a lot of lines, just for the popups. I added the popups just to show some of the properties you could use for filtering.
    Only 10 lines are realy needed to delete the old files.

    If the files to be deleted are also in subfolders, then you have to create two For-Next loops. One to pass every subfolder and the other one for the files in each of the subfolders.


    \Rems
    Last edited by Rems; 28th August 2007, 23:50.

    This posting is provided "AS IS" with no warranties, and confers no rights.

    __________________

    ** Remember to give credit where credit's due **
    and leave Reputation Points for meaningful posts

    Comment


    • #3
      Re: Script for 1 month retention

      In case you want it, here is the script that we use. There are two things you would need to change. The RetentionPeriod and the path to the folder. I use this script on about 10 different servers. It is setup as a scheduled task on each individual machine.

      Code:
      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 'This is where you tell it how many days to keep
      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
      Else
        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")'Enter the path to the files.  
      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
        Else
          FileJulianDay = FileJulianDay
        End If
        FileDateStamp = Int(FileYear & FileJulianDay)
        DateDifference = SystemDateStamp - FileDateStamp
        If (DateDifference > 365) Then
          DateDifference = DateDifference - 365
        End If
        If DateDifference > RetentionPeriod Then
          objFile.Delete()
        End If
      Next
      Just another way to do what you want.

      Enjoy

      app

      Comment


      • #4
        Re: Script for 1 month retention

        Thank you bot for reply... apperrault, i beleive your script will work for me... I just need some help editing... The logs get saved in folders and within the folder are the .txt and .csv files (which are the logs). A folder gets created each day... The format of the folder is year month day... for example 20070828 is todays folder... What i need to do is to delete the daily created folders and everything in them... Such as delet folder 20070828...

        Comment


        • #5
          Re: Script for 1 month retention

          Found the following vbs script on http://lifehacker.com/software/lifeh...ipt-288443.php
          This does exactly what i am looking for. Thanks both for your help.

          '''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''''''''''''''''''''''''''''''''''''
          'DESCRIPTION: This script will clean up any files that have a last modified
          ' date greater than or equal to the number specified.
          'CREATED BY: Brian Plexico - microISV.com
          'MODIFIED BY: Gina Trapani ([email protected])
          'CREATE DATE: 07/19/2005
          'UPDATED: 08/10/2007
          '
          'INSTRUCTIONS: 1. Enter the path to the directory you'd like to clean on line 28.
          ' Make sure to only change the text that currently reads:
          ' "C:\DIRECTORY_TO_CLEAN". The path must be surrounded by quotes.
          ' 2. Enter the number of days that must have passed for a file to be deleted
          ' on line 38. The default is 30 but can be changed to any number.
          ' 3. If you want to retain some of the subfolders so that they aren't deleted,
          ' you have two choices. See A & B below.
          ' A. Add the folder names you want to skip to the foldersToSkip line.
          ' Separate foldernames with a semicolon.
          ' Example: foldersToSkip = "Folder1;Folder2"
          ' B. Create a file named 'janitor.skip' in the folder(s) you want to
          ' leave alone.
          '
          '
          'LEGAL MUMBO JUMBO: -THIS SCRIPT HAS THE POTENTIAL TO BE VERY DAMAGING TO YOUR COMPUTER.
          ' -USE THIS SCRIPT AT YOUR OWN RISK.
          ' -I'M NOT RESPONSIBLE FOR ANY BAD EFFECTS OF THIS SCRIPT SUCH AS DIZZINESS,
          ' NOSE BLEEDS, OR A COMPLETELY DESTROYED OPERATING SYSTEM.
          ' -IF YOU HAVE DOUBTS AS TO WHAT YOU'RE DOING, THEN DON'T DO IT.
          '''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''''''''''''''''''
          'DO NOT EDIT THE SECTION BELOW '''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''
          Option Explicit
          On Error Resume Next

          Dim fso, PathToClean, numberOfDays, folder, rootFolder, objFolder, objSubfolders, objFiles, folderToClean, folderToCheck, fts, foldersToSkip, skippedfolders
          Set fso = CreateObject("Scripting.FileSystemObject")
          Set fts = CreateObject("Scripting.Dictionary")
          'DO NOT EDIT THE SECTION ABOVE ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^

          '''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''''''
          'ENTER THE PATH THAT CONTAINS THE FILES YOU WANT TO CLEAN UP
          '''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''''''
          'Path to the root directory that you're cleaning up
          PathToClean = "C:\logs"
          '^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^

          '''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''''''
          'ENTER THE NUMBER OF DAYS SINCE THE FILE WAS LAST MODIFIED
          '
          'ANY FILE WITH A DATE LAST MODIFIED THAT IS GREATER OR EQUAL TO
          'THIS NUMBER WILL BE DELETED.
          '''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''''''
          'Specify the how many days old a file must be in order to be deleted.
          numberOfDays = 20
          '^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^

          '''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''''''
          'ENTER THE NAMES OF THE FOLDERS YOU DO NOT WANT TO BE DELETED
          '
          'ALL FOLDERS WITH THE SPECIFIED NAME WILL NOT BE DELETED.
          'ALL FILES IN THE SPECIFIED FOLDERS WILL NOT BE DELETED.
          '''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''''''
          ' INSTRUCTIONS TO ADD FOLDERS YOU WISH TO SKIP
          'Add the names of the folders you wish to skip.
          'Separate each name with a semicolon.
          'If you don't wish to use this feature, the line below must look like this: foldersToSkip = ""
          'The line must look like the following to skip folders: foldersToSkip = "Folder1;Folder2"

          foldersToSkip = ""

          '^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^



          '^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^
          '''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''''''''''''''''''''' '''
          'DON'T CHANGE ANYTHING BELOW THIS LINE
          '''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''''''''''''''''''''' '''

          'Check to make sure path is not a drive root
          If Right(PathToClean, 2) = ":\" or Right(PathToClean, 1) = ":" Then
          msgbox "Whoa Nelly! It's best not to run the Janitor on a drive root like " + PathToClean, vbOkOnly, "Don't Do That!"
          End If

          '''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''''''
          'Start at the folder specified and walk down the directory tree
          '''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''''''
          Set rootFolder = fso.GetFolder(PathToClean)
          If Err.Number > 0 Then
          msgbox PathToClean + "is not a valid directory path. Please correct the path and run the script again.", vbOkOnly, "Path Not Found"
          Wscript.Quit
          End If

          EnumerateFoldersToSkip(foldersToSkip)
          GetSubfolders(rootFolder)
          CleanupFiles(rootFolder)

          'Let person know when the cleanup is complete
          MsgBox "Files older than " + CStr(numberOfDays) + " days have been deleted from " + PathToClean, vbOkOnly, "Windows Janitor Cleanup Complete"

          'Clean up
          Set fso = Nothing

          Wscript.Quit
          '''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''''''

          Sub EnumerateFoldersToSkip(skippedfolders)
          If skippedfolders <> "" Then
          If InStr(1, skippedfolders, ";", 1) <> 0 Then
          Dim arrSkippedFolders, sf
          arrSkippedFolders = Split(skippedfolders, ";")
          For each sf In arrSkippedFolders
          fts.Add UCase(Trim(sf)), ""
          Next
          Else
          fts.Add UCase(skippedfolders), ""
          End If
          End If
          End Sub

          Sub GetSubfolders(folder)
          If CheckForSkip(folder) Then Exit Sub

          Dim oSubfolder
          Set objFolder = fso.GetFolder(folder)
          Set objSubfolders = objFolder.Subfolders

          For Each oSubfolder in objSubfolders
          If fts.Exists(UCase(oSubfolder.Name)) = False Then
          'Recursively go down the directory tree
          GetSubfolders(oSubfolder.Path)

          'Cleanup any files that meet the criteria
          CleanupFiles(oSubfolder.Path)

          'Delete the folder if its empty
          CleanupFolder(oSubfolder.Path)
          End If
          Next
          End Sub

          Sub CleanupFiles(folderToClean)
          If CheckForSkip(folderToClean) Then Exit Sub

          dim objFile
          Set objFolder = fso.GetFolder(folderToClean)
          Set objFiles = objFolder.Files

          For Each objFile in objFiles
          If DateDiff("d", objFile.DateLastModified, Now) > numberOfDays Then
          objFile.Delete
          End If
          Next

          Set objFolder = Nothing
          Set objFiles = Nothing
          End Sub

          Sub CleanupFolder(folderToCheck)
          If CheckForSkip(folderToCheck) Then Exit Sub

          Set objFolder = fso.GetFolder(folderToCheck)
          Set objSubfolders = objFolder.Subfolders
          Set objFiles = objFolder.Files

          If objFiles.Count = 0 and objSubfolders.Count = 0 Then
          objFolder.Delete
          End If

          Set objFolder = Nothing
          Set objSubfolders = Nothing
          Set objFiles = Nothing
          End Sub

          Function CheckForSkip(folderToCheck)
          CheckForSkip = fso.FileExists(folderToCheck & "\JANITOR.SKIP")
          End Function

          Comment

          Working...
          X