FTP Cleanup Script

By | November 1, 2007

We have a couple FTP servers here at my job and there are files which have been in these directories for years. The company policy stated that files older than 30 days must be deleted. Some people follow it and others don’t. So I decided to help them out.

This script will clean out all files, NOT FOLDERS, under the specified directory where the date created is older than the maximum age in days specified.


On Error Resume Next
‘Start Script Configuration—————————————————————————-
FolderName = “E:\FTPSite\”
Days = 30
dt = Replace(Date,”/”,”-“)
‘End Script Configuration——————————————————————————

Set objFSO = CreateObject(“Scripting.FileSystemObject”)   
Set objTextFile = objFSO.CreateTextFile( “C:\FTPCleanup”& dt &”.log”)

set fso = createobject(“scripting.filesystemobject”)
set folders = fso.getfolder(FolderName)
datetoday = now()
newdate = dateadd(“d”, Days*-1, datetoday)

objTextFile.WriteLine “Today: ” & now()
objTextFile.WriteLine “Started deleting files older than:” & ” ” & newdate
objTextFile.WriteLine “________________________________________________”
objTextFile.WriteLine “”
recurse folders
objTextFile.WriteLine “”
objTextFile.WriteLine “Completed deleting files older than:” & ” ” & newdate
objTextFile.WriteLine “________________________________________________”

sub recurse( byref folders)
set subfolders = folders.subfolders
set files = folders.files
objTextFile.WriteLine “”
objTextFile.WriteLine “Deleting Files under the Folder:  ” & ” ” & folders.path
objTextFile.WriteLine “__________________________________________________________________________”
for each file in files
if file.datecreated < newdate then
objTextFile.WriteLine “Deleting ” & folders.path & “\” & file.name & ” Created:” & ” ” & file.datecreated
objTextFile.WriteLine strComputer & “;” & dtmSystemUptime & ” Hours”

on error resume next
end if


for each folder in subfolders
recurse folder

set subfolders = nothing
set files = nothing

end sub

‘Start SMTP Configuration——————————————————————————
strFrom = “FTPCleanup@domain. com”
strTo = “email1@domain. com, email2@domain. com”
strSub = dt & ” FTP Server Cleanup Report”
strBody = “Please see the attached FTP Cleanup report”
strSMTP = “email.domain.com”
‘End SMTP Configuration——————————————————————————–

set objEmail = CreateObject(“CDO.Message”)
objEmail.From = strFrom
objEmail.To = strTo
objEmail.Subject = strSub
objEmail.Textbody = strBody
objEmail.AddAttachment “C:\FTPCleanup”& dt &”.log”
objEmail.Configuration.Fields.Item(“http://schemas.microsoft.com/cdo/configuration/sendusing”) = 2
objEmail.Configuration.Fields.Item(“http://schemas.microsoft.com/cdo/configuration/smtpserver”) = strSMTP
‘WScript.Echo “Email sent”


There are 2 parts to this script, one is the cleanup configuration and the other is the Email configuration.

The first part you need to configure is the Cleanup settings.

FolderName = “E:\FTPSite\”
Replace “E:\FTPSite\” with the path to your FTP Root Directory

Days = 30
Replace 30 with how many days old you want to start deleting after

dt = Replace(Date,”/”,”-“)
Dont touch this one, this one formats the date and time for the cleanup report

If you do not need this to send an email confirmation, simply delete from
‘Start SMTP Configuration——————————————————————————

to the end of the script

If you do want to send the cleanup report to someone, simply fill in with your from, to, email subject, email body, and email server.

This script delete ONLY FILES, NOT FOLDERS. This script also only cares about files older than the time specified. It will delete ALL FILES that mean its criteria.

I also chose to go with date created because if you go off of modified date, if you take a file from 3 years ago and put it in the cleanup folder, it will get deleted the first time the script runs because the last time the file was modified was 3 years ago but when you copy it to a new server, a new Date created gets created.


This script is provided “AS IS” with no warranties expressed or implied.

Leave a Reply

Your email address will not be published. Required fields are marked *