'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'Copyright Notice : (2003)This code has been written by Adwait Joshi. This is a freeware and  '
'you can use the code as you like or modify the code as you like as long as you preserve this '
'tag. Thanks for your cooperation.  									    '
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

Dim strFile                     'The fileon which currently working
Dim strDateCreated              'Date when the file was created
Dim strDateOld                  'Used to get the value of todays date minus the days old constant
Dim CurrentFile                 'File which is currently worked on
Dim MyFolderList                'Used to split the folders in an array
Dim x                           'Count for folder list array
Dim strWorkingDir               'Current working directory
Dim strDateCount                'To count the files that are x days old. 
Dim Days2Old                    'Used to convert the constant days old into a negative number
Dim strFileType                 'Grab the Array from DayOld
Dim strFileArr                  'File type array
Dim ArrayFiles                  'Array of Files Deleted
Dim SplitArray                  'Variable to hold the split arrays
Dim strNetIQ



CONST RootDirectory = "c:\temp"				'This is the directory form where you want to delete the files
CONST FileType = "txt"						'This is the file type. If there are more than one type then say "java,jar,class"
CONST DaysOld = 1							'This tells the code that how old should be the files to be deleted
CONST DeleteAllFiles = "n"					'If this variable is y then it doesnt check for dates simply deletes all the files




'Function to display the file info
Function ShowFileInfo(strFile)
         'Declaring necessary variables
         Dim fso,f
         Dim strOld
         'Getting handles
         set fso = CreateObject("Scripting.FileSystemObject")
         set f = fso.GetFile(strFile)
         'To see when the file was last modified
         strDateCreated = FormatDateTime(f.DateLastModified)
End Function

'Function to delete the selected file
Function DeleteOldFile(strFile)
         'Declaring necessary variables
         Dim fso,f
         Dim strDeletedFile
         'Getting Handles
         Set fso = CreateObject("Scripting.FileSystemObject")
         Set strDeletedFile = fso.GetFile(strFile)
         strDeletedFile.Delete

End Function

'Main function to get the dates. This is where the paths get appended and then 
'the date are calculated. i.e. if the file is older or not
Function GetCreatedDate(strFileCreated)

         'Setting the file string to the current file path and name
         strFile = RootDirectory & "\" & strFileCreated
         'Getting the Information for this file
         Call ShowFileInfo(strFile)
         'Formatting the date using the short date format specified in your computer's regional settings.
         GetCurrentDate = FormatDateTime(strdatecreated,2)
         'Freeing the current date string
         strDateCreated = GetCurrentDate
         'Storing todays date in GetCurrentDate
         GetCurrentDate = Date
         'Converting into negative
         Days2Old = 0 - DaysOld
         'Add date is a funcion used to add to the current date. In this case "d" is days and
         'Days2Old is negative so actually subtracting the days from current date variable
         strDateOld = dateadd("d",Days2Old,GetCurrentDate)
         'Calculating the difference in days
         CurrentMonth = dateDiff("d", strDateCreated, GetCurrentDate)
         'If this condition is met the file will be deleted
         if CurrentMonth >= DaysOld then
            strDateCount = strDateCount + 1
            ArrayFiles = ArrayFiles + "," + strFile
            'Calling the actual Delete File Function
            call DeleteOldFile(strFile)
         end if
End Function


'Function to get the list of files in a folder. Folder is the folder is the root directory
Function GetFileList(RootDirectory)
        'Declaring the variables here
  	Dim fsof, fi, flf, sf, fc
	Dim strFileSplit
	
	'Getting the handles here
	Set fsof = CreateObject("Scripting.FileSystemObject")
	Set fi = fsof.GetFolder(RootDirectory)
	Set fc = fi.Files
	'For each file name do this
	for each flf in fc
		sf = sf & flf.name
		'Splitting the file name into file name and file extension
		strFileSplit = split(sf,".")
                'If the extension of the file is equal to the FileType extension speficied then do this
		'if strFileSplit(1) = strFileType then
                        CurrentFile = sf
                        'Calling to see when this file was created
			call GetCreatedDate(CurrentFile)
		'end if
		'Making sf empty otherwise it will go on appending
		sf = ""
	next
end Function

'This function will delete all the files if a "y" is specified in deleteallfiles
Function GetAllFilesToDelete(RootDirectory)
         'Declaring the variables here
         Dim fsof,fi,flf,sf,fc
         Dim strFileSplit
         'Getting the file handles here
         Set fsof = CreateObject("Scripting.FileSystemObject")
         Set fi = fsof.GetFolder(RootDirectory )
         Set fc = fi.Files
         'For each file do this
         For Each flf in fc
             sf = sf & flf.name
             strFileSplit = split(sf,".")
             'Delete all the files of specified extension
'             if strFileSplit(1) = strFileType then
                CurrentFile = sf
                strFile = RootDirectory & "\" & CurrentFile
                ArrayFiles = ArrayFiles & "," & strFile
                Call DeleteOldFile(strFile)
                strDateCount = strDateCount + 1
 '            end if
             sf = ""
         next
End Function

'This is the main sub routine

'Creating a array depending upon user specified extensions. Splitting the arrays on ","
strFileArr = split(FileType, ",")
strFileType = UBound(strFileArr)

strDateCount = 0

'If there is only 1 extension in the user supplied extensions strFiletype = FileType
If strFileType = 0 then
    strFileType = FileType
    'Depending upon DeleteAllFiles is "y" or "n" call the specific function
    If DeleteAllFiles = "y" or DeleteAllFiles = "Y" then
        Call GetAllFilesToDelete(RootDirectory)
    Else
        Call GetFileList(RootDirectory)
    End If
Else
    'For each extension specified do this
    For i = 0 to UBound(strFileArr)
        strFileType = strFileArr(i)
        'Depending upon DeleteAllFiles is "y" or "n" call the specific function
        If DeleteAllFiles = "y" or DeleteAllFiles = "Y" then
           Call GetAllFilesToDelete(RootDirectory)
        Else
           Call GetFileList(RootDirectory)
        End If
    Next
End IF

'This MessageBox will tell you what files were delete. If you dont want to specify a message 
'box take this part of the code out. For example if this is a script running on server you
'dont want to show a message box. But in case its executed by a user then show him the status
'i.e. if the files were deleted or not and if they were, which files were deleted
'if strDateCount = 0 then
'   msgbox "0 Files were deleted!"
'else
'    splitArray = replace(ArrayFiles,",",Chr(10) & Chr(13))
'    strNetIQ = strDateCount & "Files were Deleted !" & Chr(10) & Chr(13) & "The following files were deleted :" & Chr(10) & Chr(13) & splitarray
'    msgbox strNetIQ
'end if


