Date and Time string for one month ago

A co-worker wanted to delete all the *.ALH alarm history files from more than a month ago, so he wrote a VBScript that is executed based on a tag set by a Calendar Event in the Scheduler task. The script used the DeleteOlderFiles function, which requires a time and date string in the format "DD/MM/YY HH:MM:SS". There are variations based on local time format but that's the basic for most USA computers.

He came up with a workable vbscript program that subtracted one from the month and then accounted for varying length months. It was clever but it looked like too much work.

This is a simplified script that uses the Clock feature, which counts the seconds from January 1, 1970. It subtracts 30 days worth of seconds from right now, then uses built-in IWS features to get a Time and a Date from that value.

There may be an even simpler method, but this one seemed nice and efficient.

Hat tip also to Miguel at IWS Support for verifying that the DeleteOlderFiles function in IWS 8.0 SP1 does need a Time stamp to work properly. It's not every company that can jump to attention and do a program feature test for you, and that's one of the reasons I use Indusoft.



// This script generates a Date and Time string for 30 days in the past
// The string can be used in conjunction with the DeleteOlderFiles IWS function
Dim MonthAgoClock
Dim MonthAgoDate
Dim MonthAgoTime
Dim MonthAgoString

// GetClock is an IWS function that returns the number of seconds since Jan1 1970
// 60 seconds x 60 minutes x 24 hours x 30 days = 2,592,000 seconds

MonthAgoClock = $GetClock() - 2592000
MonthAgoDate = $ClockGetDate(MonthAgoClock)
MonthAgoTime = $ClockGetTime(MonthAgoClock)
MonthAgoString = MonthAgoDate & " " & MonthAgoTime

  • For Using File System Object Scripting: (A little more indepth, but full windows based)
    Function funcDeleteLogs
    'Description: Search Log File Folders and Copy/Delete/Create
    'Function Number: 22
    'Parameters: N/A
    'External Variables:
    'History:
    ' 16 May 2013 - Creation of the Function by Alan Cannon
    'On Error Resume Next
    'Variable Declaration
    Dim objFSO,colFiles,objFile,objFolder,FolderExists
    Dim DirToSearch,DirToMove,dtmMonthAgo,dtmDaysAgo
    Dim FileToSearch,FilestoDelete
    Dim FolderMain,FolderSub,FolderExistsMain,FolderExistsSub

    FolderMain = "C:\HMILOGS"
    FolderSub = "C:\HMILOGS\BACKUPS"
    DirToSearch = "C:\HMILOGS\"
    DirToMove = "C:\HMILOGS\BACKUPS\"
    FileToSearch = "INDUSOFT_TRACELOG_*.txt"
    FilestoDelete = "*.txt"
    $TRACELOG_NAME = "C:\HMILOGS"+"\INDUSOFT_TRACELOG"+$LogDateFormat+".txt"
    'Inform the Beginning Of the action
    funcLog 1,"funcDeleteLogs","Execution Started"
    On Error Resume Next
    'check If the Folder Paths Exists If Not Create Them To avoid Failures
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    FolderExistsMain = objFSO.FolderExists(FolderMain)

    If Not FolderExistsMain Then
    FuncLog 1,"funcDeleteLogs","Creating Main Log Folder Someone Deleted Me"
    objFSO.CreateFolder FolderMain
    End If
    FolderExistsSub = objFSO.FolderExists(FolderSub)
    If Not FolderExistsSub Then
    FuncLog 1,"funcDeleteLogs","Creating Sub Log Folder Someone Delete Me"
    objFSO.CreateFolder FolderSub
    End If

    'Enumerate through Files to Copy First
    Set objFolder = objFSO.GetFolder(DirToSearch)
    Set colFiles = objFolder.Files

    dtmDaysAgo = DateAdd("d", -1, Now)

    For Each objFile In colFiles
    If objFile.DateCreated < dtmDaysAgo Then
    objFSO.CopyFile objFile.Path, DirToMove, True
    objFSO.DeleteFile objFile
    End If
    Next

    'Check if Files in BackupFolder are Older than 1 Months then Delete These
    Set objFolder = objFSO.GetFolder(DirToMove)
    Set colFiles = objFolder.Files

    dtmMonthAgo = DateAdd("m", -1, Now)

    For Each objFile In colFiles
    If objFile.DateCreated < dtmMonthAgo Then
    objFSO.DeleteFile DirToMove+FilestoDelete
    End If
    Next

    'Clean Up Fso objects
    Set objFile = Nothing
    Set colFiles = Nothing
    Set objFolder = Nothing
    Set objFSO = Nothing
    'Inform the End Of the action
    funcLog 1,"funcDeleteLogs","Execution Completed"
    End Function

  • Thanks for posting that script Alan, it's quite nice.