Delete files older than. . .

All questions related to installations, configurations and maintenance of Advanced Host Monitor (including additional tools such as RMA for Windows, RMA Manager, Web Servie, RCC).
Post Reply
User avatar
greyhat64
Posts: 246
Joined: Fri Mar 14, 2008 9:10 am
Location: USA

Delete files older than. . .

Post by greyhat64 »

I'm not seeing it, so it must be there. . . :o
I see where the Count Files test can note "Files older than xx days".
Can this be used to execute a delete action on any files that meet the criteria, or is this going to be another script writing exercise?
It would even help if the test could generate a 'file list' that could be passed to an action script.
Regards,
Joel
KS-Soft
Posts: 13012
Joined: Wed Apr 03, 2002 6:00 pm
Location: USA
Contact:

Post by KS-Soft »

If HostMonitor can provide such list, you still need to create script to process the list and remove the files. So, why do the same job twice?
I think you may create script that will remove old files. Something like this

Code: Select all

const TargetFolder = ""c:\targetfolder\"
const MaxAge = 60

Dim FSO, fsFolder, item
Set FSO = CreateObject("Scripting.FileSystemObject") 
Set fsFolder = fso.GetFolder(TargetFolder) 
For each item in fsFolder.Files
  If DateDiff("n", Item.DateLastModified, Date+Time)>MaxAge 
     Then Item.Delete
  End If
Next
Regards
Alex
Paul_NHS
Posts: 59
Joined: Wed Feb 25, 2009 6:17 am

Post by Paul_NHS »

We use this.

cheers, Paul

Code: Select all

Option Explicit
' **
------------------------------------------------------------------------
' ** -- Description:    Deletes files older than a specified number of days
' ** --                 from the folder identified.
' ** --
' ** -- Parameters:        1 - The folder that is to cleaned out
' ** --                    2 - The number of days old the file must be before
' ** --                        they are deleted.
' ** --                   
' ** -- e.g. To clean out files older than 5 days from the c:\temp folder you would have:
' ** -- cscript.exe "path\CleanUpFolder.vbs" c:\temp 5
' ** --
' ** -- Called by:     
' ** -- Calls:         
' ** --
' ** -- Created:          16/07/03 by Paul Pettigrew
' ** --
' ** -- Modified:   
' ** --
' **
------------------------------------------------------------------------

Dim objFSO
Dim objFolder
Dim objFileCollection
Dim objFile
Dim colArguments
Dim strFolderPath
Dim strDays
Dim intDays
Dim blnContinue
Dim dteCutoff


blnContinue = False

' ** -- Read in the Command line parameters 
Set colArguments = Wscript.Arguments
If colArguments.Count = 2  Then
    strFolderPath = colArguments(0)
    strDays = colArguments(1)

    ' ** -- Check that the parameters look reasonable
    If Len(strFolderPath) > 0 Then
        If Len(strDays) > 0 Then
            intDays = 0 - CInt(strDays)
            blnContinue = True
        End If
    End If
End If

If blnContinue = True Then
    Set objFSO = CreateObject("Scripting.FileSystemObject")

    ' ** -- Check if the specified Folder exists
    If objFSO.FolderExists(strFolderPath) Then
        Set objFolder = objFSO.GetFolder(strFolderPath)
        Set objFileCollection = objFolder.Files

        ' ** -- Calculate the Cutoff date
        dteCutoff = DateAdd("D", intDays, Now)
       
        ' ** -- Loop through the Folder looking for the old files
        For each objFile in objFileCollection
            If objFile.DateLastModified < dteCutoff Then
                objFile.Delete True
            End If
        Next
       
        ' ** -- Clean Up
        Set objFile = Nothing
        Set objFileCollection = Nothing
        Set objFolder = Nothing
    End If
   
    Set objFSO = Nothing
End If
User avatar
greyhat64
Posts: 246
Joined: Fri Mar 14, 2008 9:10 am
Location: USA

Post by greyhat64 »

Paul,
You never cease to amaze me!

After a discussion with the requester I'm changing strategies. This group does 'mock ups' in this Temp folder which is comprised of a bunch of subfolders. Because of the way it is used there may be file dependencies, so simple creation/modification dates are unusable, lest you break up the 'set'.

I haven't worked out all the details but my current logic is to look at top level folder creation/modification date, but even that is not entirely reliable. Once I've found a 'stale' folder the action will be to notify the owner/manager. I've yet to figure out what to do from there. I guess I could nag them incessantly until they delete/move it.

What a kludge. :-?

Alex,
Maybe Paul would let you include this script in the list of standard shell scripts for v8 if you are real nice to him.
I'd like to se a growing list of 'test method specific' actions like this included - either as shell script examples, or as builtin actions associated with the test method.
KS-Soft
Posts: 13012
Joined: Wed Apr 03, 2002 6:00 pm
Location: USA
Contact:

Post by KS-Soft »

We don't want to include such script as "Shell Script" test method. No way!
It removes files and I am sure some people will remove important files from the system while testing HostMonitor.
TEST should NOT modify system!! Test should check the system without changing any settings.

Regards
Alex
User avatar
greyhat64
Posts: 246
Joined: Fri Mar 14, 2008 9:10 am
Location: USA

Post by greyhat64 »

Ok, Ok - it was just a suggestion :-?
But I will say that I'm probably not alone in looking at every test method and thinking, "What can I DO with it? What kind of action can I take based on the results?" This is just one example of an action that can be taken based on a simple file/folder examination. The more examples given to the new user the broader/deeper the adoption of the product.
I love AHM and I really like what Paul did with this script - I was just showing some appreciation.
Paul_NHS
Posts: 59
Joined: Wed Feb 25, 2009 6:17 am

Post by Paul_NHS »

Thanks for the compliment, but I can't even claim this one as mine - although I did ask the bod who wrote it to write it (is that good English?!). I just use it everywhere I go - the joys of contracting ;)

cheers, Paul
Robert_in_MTL
Posts: 229
Joined: Tue Jun 20, 2006 1:20 pm
Location: Montreal, Quebec

Post by Robert_in_MTL »

Hi Sorry to revive this old post...
I am trying to implement this and I am almost successful lol

I want to delete older backups in a folder but make sure that I keep at least x number of files in case the backups cease to be done, ( keeps at least a number of backups)

I installed a local agent on the server where the backups are, lets say d:\backups\

test#1: executed by local agent, count files, if filecount is more then X , become bad

test #2: executed by local agent, Active Script, depends on test #1, execute script described above like this:

D:\IT\Scripts\CleanOldFiles.vbs D:\CISCO_Backups 5


If I execute it, it returns: Cannot open file

"C:\WINDOWS\system32\"D:\IT\Scripts\CleanOldFiles.vbs" D:\CISCO_Backups 5". The filename, directory name, or volume label syntax is incorrect

I know I am close and it's maybe just a tweak I need to do, do you have a hint for me?

Thanks
KS-Soft
Posts: 13012
Joined: Wed Apr 03, 2002 6:00 pm
Location: USA
Contact:

Post by KS-Soft »

Its better to create new thread than find some 9 years old...

So you set "D:\IT\Scripts\CleanOldFiles.vbs" as parameter of Execute external program action? While RMA returns error related to "C:\WINDOWS\system32\"D:\IT\Scripts\CleanOldFiles.vbs"?
RMA version?

Regards
Alex
Robert_in_MTL
Posts: 229
Joined: Tue Jun 20, 2006 1:20 pm
Location: Montreal, Quebec

Post by Robert_in_MTL »

KS-Soft wrote:Its better to create new thread than find some 9 years old...
Yes, I know, found it by searching and it is directly related to the script Paul posted...
KS-Soft wrote: So you set "D:\IT\Scripts\CleanOldFiles.vbs" as parameter of Execute external program action? While RMA returns error related to "C:\WINDOWS\system32"D:\IT\Scripts\CleanOldFiles.vbs"?
RMA version?

Regards
Alex
RMA is 6.10, runs as service as administrator from a domain account

I need to send parameters to the script

The parameter is exactly this:
D:\IT\Scripts\CleanOldFiles.vbs D:\CISCO_Backups 5

but I tried
"D:\IT\Scripts\CleanOldFiles.vbs" D:\CISCO_Backups 5

and
"D:\IT\Scripts\CleanOldFiles.vbs D:\CISCO_Backups 5"


The test itself replies

Code: Select all

cannot open file "D:\IT\scripts\cleanoldfiles.vbs D:\CISCO_Backups 5". the filename, directory name, or volume label syntax is incorrect
If I only type D:\IT\Scripts\CleanOldFiles.vbs, it still does not find it
KS-Soft
Posts: 13012
Joined: Wed Apr 03, 2002 6:00 pm
Location: USA
Contact:

Post by KS-Soft »

Sorry, its still unclear what exactly command line is used, could you please send screen shot of the action settings to support@ks-soft.net?)
(HostMonitor and RMA cannot process scripts, so normally you start vbs scripts using cscript.exe command)

Regards
Alex
Robert_in_MTL
Posts: 229
Joined: Tue Jun 20, 2006 1:20 pm
Location: Montreal, Quebec

Post by Robert_in_MTL »

ah!
there you have it, I must use cscript
I was calling the .vbs script directly.

I will search for that in the helpfile and try to make it work
Thanks
Post Reply