Friday, July 9, 2010

VBscript: Delete Files older then X Days


' ################################################################
' # DelOldFiles.vbs
' # Removes all files older than X Days
' # John Minkjan
' # Ciber Netherlands
' # Execute: cscript.exe  deloldfiles.vbs "H:\BACKUP\DEV\" 1
' ################################################################
Dim fso, f, f1, subF, fc, strDirSub
' user variables
' —————————————————————-
'# Read the command line
dim command_line_args
set command_line_args = wscript.Arguments
strDir = command_line_args(0)
strDays = command_line_args(1)
'# Traverse Folders and delete ol files
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFolder(strDir)
For each item in f.SubFolders
    strDirSub = item.Path
    Set subF = fso.GetFolder(strDirSub)
    wscript.echo strDirSub
    Set fc = subF.Files
    For Each f1 in fc
        If DateDiff("d", f1.DateCreated, Date) > cint(strDays) Then
            fso.DeleteFile(f1)
        End If
    Next
Next

WScript.Quit

Welcome to VBscript 101

Hello World