Further Automate Disk Clean-up by Scripting

April 29, 2009 · Print This Article

 
 

For those of you who have a need to keep your server or workstation tidy, employing an automatic file deletion script may be right up your alley. Take for instance, your web logs, sql logs, or Windows temp files. All of these files can get really out of hand if not kept in check. You also may want to keep a shared folder on your server for “file swapping” with in your local LAN. If that is the case wouldn’t you like housekeeping to be done automatically? Letting your users know it is a temporary 14 or 7 day directory may even reduce your inneroffice exchange attachments or present itself as an alternative to failed large attachement size.

Below is a script that will allow you to specify the directory paths and the number of days from the date the script is run to retain data. If you want to delete all temporary files older than 7 days, your script will look like this: cscript DeleteOldFiles.vbs C:\windows\temp 7
Obviously the meat of the script is in the “DeleteOldFiles.vbs”, which is provided below.

If you have more than one directory you need to delete files in, simply duplicate your call and replace the directory path.

Now for the Script:
option explicit

Call DoTheJob()
WScript.Echo "--- end of script execution ---"

Sub DoTheJob
dim limitDate
dim formattedLimitDate
dim folder
dim strComputer
dim objWMIService
dim colFileList
dim objFile
dim nbFiles
dim totalFiles
dim nbErrors
dim result
dim nbDays

if WScript.Arguments.Count <> 2 then
WScript.Echo “usage : DeleteOldFiles.vbs
WScript.Echo “sample: DeleteOldFiles.vbs C:\Windows\temp 90″
Exit Sub
end if

folder = WScript.Arguments(0)
nbDays = WScript.Arguments(1)

‘calculate and format limit date
limitDate = DateAdd(”d”, -1 * nbDays , Date)

formattedLimitDate = DatePart(”yyyy”, limitDate)

if DatePart(”m”, limitDate) < 10 then
formattedLimitDate = formattedLimitDate & "0"
end if
formattedLimitDate = formattedLimitDate & DatePart("m", limitDate)

if DatePart("d", limitDate) < 10 then
formattedLimitDate = formattedLimitDate & "0"
end if
formattedLimitDate = formattedLimitDate & DatePart("d", limitDate)

'show what will be done
WScript.Echo "Will remove files from " & folder & " with a date older than " & formattedLimitDate & " (" & nbDays & " days ago)"

'Get the files and delete the old ones
strComputer = "."

Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colFileList = objWMIService.ExecQuery _
("ASSOCIATORS OF {Win32_Directory.Name='" & folder & "'} Where " _
& "ResultClass = CIM_DataFile")

nbFiles = 0
totalFiles = 0
nbErrors = 0

For Each objFile In colFileList
totalFiles = totalFiles + 1
if objFile.CreationDate < formattedLimitDate then

result = objFile.Delete()

WScript.Echo objFile.Name & " - " & objFile.CreationDate & ". Delete Result: " & result
if result = 0 then
nbFiles = nbFiles + 1
else
nbErrors = nbErrors + 1
end if
end if
Next

'Show the result
Wscript.Echo "Total files in folder: " & totalFiles
WScript.Echo "Deleted files: " & nbFiles
WScript.echo "Errors: " & nbErrors
End Sub

Now the batch file that calls the clean up script:
cscript DeleteOldFiles.vbs C:\windows\temp 90

Here you will specify the path(s) you would like to clean up files and the age of the files to clean up.

Source cod provided by: http://blogs.msdn.com/benjguin/archive/2006/12/01/delete-old-files-script.aspx

StumbleUpon It!

Related Topics

Comments

Please leave us your comments.