I have some backup files which are created in a daily manner. After some time they take a lot of space and want to delete them according to their time stamp like delete 1 month olders. Thanks

Recommended Answers

I had to set up a batch file to do this years ago in vbScript. Now I would write it in Python but here is the original. I suggest you try it in a test folder to make sure it does what you want.

'                                                                                        '
'  Name:                                                                                 '
' …
Jump to Post

All 2 Replies

Heym
Have you tied:
find /your/folder -mtime +182

When you ready to delete use this (be careful)
find /your/folder -mtime +182 -exec rm {} +

Please be careful!

I had to set up a batch file to do this years ago in vbScript. Now I would write it in Python but here is the original. I suggest you try it in a test folder to make sure it does what you want.

'                                                                                        '
'  Name:                                                                                 '
'                                                                                        '
'    delete_older_than.vbs                                                               '
'                                                                                        '
'  Description:                                                                          '
'                                                                                        '
'    This script deletes all files more than the given number of days old.               '
'                                                                                        '
'  Usage:                                                                                '
'                                                                                        '
'    delete_older_than ## [folder] [-r] [-d]                                             '
'                                                                                        '
'    ##            is a number of days.                                                  '
'    folder        is the folder (current by default) to do the deletes in               '
'    -d            delete (default is list files to be deleted but don't delete them)    '
'    -r            recursively delete in all subfolers                                   '
'                                                                                        '
'  Note:                                                                                 '
'                                                                                        '
'    You are not asked to verify the delete so use caution.                              '
'                                                                                        '
'  Audit:                                                                                '
'                                                                                        '
'    1999-10-06  rj  original code                                                       '
'                                                                                        '

set wso = CreateObject("Wscript.Shell")
set fso = CreateObject("Scripting.FileSystemObject")
set arg = Wscript.Arguments

'check the command line arguments

if arg.count = 0 then
    howto = array( _
        "delete_older_than ## [folder] [-r]", _
        "", _
        "  ##       files older than this number of days are deleted", _
        "  folder   root folder (current by default) to do the deletes in", _
        "  -d       delete the files (default is list files to be deleted)", _ 
        "  -r       recursive delete on all sub-folders", _
        "", _
        "  You are not asked to verify the delete so use caution")
    wscript.echo join(howto,vbcrlf)
    wscript.quit
end if

'set defaults

numdays = -1
recurse = false
delete  = false
folder  = fso.GetAbsolutePathName(".")

'get any values specified on the command line

for i = 0 to arg.count - 1

    select case true
        case isnumeric(arg(i))
            numdays = cint(arg(i))
        case arg(i) = "-r"
            recurse = true
        case arg(i) = "-d"
            delete  = true
        case else
            folder = arg(i)
    end select

next

'check for missing or invalid (less than zero) numdays parameter

if numdays < 0 then
   wscript.echo "You didn't specify the number of days"
   wscript.quit
end if

'determine the cutoff date (the oldest date retained)

cutoff = cstr(DateAdd("d",-numdays,date()))

'clean up the given folder

call CleanFolder(folder,cutoff,recurse,delete)

' This function does the actual deleting. It was made into a separate function so that   '
' it could be called recursively if the -r option was selected.                          '

sub CleanFolder (ByVal foldername, ByVal cutoff, ByVal recurse, ByVal delete)

    dim fold        'the current folder object                        '
    dim file        'for stepping through the files collection        '
    dim files       'the files collection within a folder             '
    dim folder      'for stepping through the subfolders collection   '
    dim folders     'the subfolders collection within a folder        '

    'get a collection of all files in the folder

    set fold  = fso.GetFolder(foldername)
    set files = fold.files

    'process all the files in the folder

    for each file in files

        datestamp = left(file.datelastmodified,10)

        if datestamp < cutoff then
            if delete then
                Wscript.Echo "deleting", file.DateLastModified, file.Path
                fso.DeleteFile file.Path
            else
                Wscript.Echo "listing", file.DateLastModified, file.Path
            end if
        end if

    next

    'process all the subfolders in the folder

    if recurse then

        set folders = fold.subfolders

        for each folder in folders
            call CleanFolder(folder.path,cutoff,recurse,delete)
        next

    end if

end sub
Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.