Tuesday, January 1, 2008

Delete files from the network based on file path and date/time parameters.

Delete files from the network based on file path and date/time parameters.

This script came in handy when I was trying to delete some old XML documents from the network. The best part about this script was we were able to schedule to execute this script from windows scheduler. I created this script with the help of Michael Scott.

'*******************************************************************************************
' Created BY : Srinivasan Prasanna
' Company : Company ABC
' Date : 09/05/2007
' Logic : This script is run as a batch program to remove the files from the
' selected folder
' : This script was created with the help of an internet resource - Michael
' Scott
'*************************************************************************************


Dim fso, fldr, fc, f1 'fldname, usrname, srcFile
set FSO = Wscript.CreateObject("scripting.FileSystemObject")
fldname = "D:\HJS\DCA\Peoplesoft89\XML\Export" '<---- change to top directory Const DateofFile = -30 '<----- age of deleted files
DeleteFiles = FSO.GetFolder(fldname)
Set fldr = fso.GetFolder(fldname)

Recurse fldr

Set fldr = Nothing
Set fso = Nothing
wscript.echo "Files Deleted Successfully"
Wscript.Quit

Public Sub Recurse( ByRef fldr)
dim subfolders,files,folder,file
Dim srcFile
Set subfolders = fldr.SubFolders
Set files = fldr.Files

For Each srcfile in files
If DateDiff("d", Now, srcFile.DateLastModified) &lt; DateofFile Then
FSO.DeleteFile srcFile, True
End If

Next

'************** Recurse all of the subfolders.

For Each folder in subfolders
Recurse folder
Next

'************** Deletes empty folders

if (fldr.files.count = 0) and (fldr.subfolders.count) = 0 then
on error resume next
fldr.delete
exit sub
end if

End Sub

You can also download this script from the google document site