Archive

Archive for June, 2011

CScript to delete html files older than one day and produce a log file


Had this challenge to delete only html files in drive C older than one day in  some servers  every Wednesday at 20:00 as well as produce a log file after the operation is  complete.

I did put this script together to delete the files and then schedule a task that will run start the  script every wednesday at 20:00.

=======================================================================================================================================================

OPTION EXPLICIT
DIM strExtensionsToDelete,strFolder, strLogName
DIM objFSO, MaxAge, IncludeSubFolders
DIM strScript, objLogFile, strLogLocation

‘ ************************************************************
‘ Setup
‘ ************************************************************
‘NB
‘ Once this script is started, it will  log  “deleting files started” and when

‘executed to the end ‘it will also  log “deleting files completed”

‘even if there are no files to delete. Will still optimize it.
‘ Folder to delete files
strFolder = “C:test”
‘ Delete files from sub-folders?

‘Set this to true in case you want to delete in sub folders as well
includeSubfolders = false
‘ A comma separated list of file extensions
‘ Files with extensions provided in the list below will be deleted
strExtensionsToDelete = “html”
‘ Logging  location
strLogLocation = “C:”
strScript = Wscript.ScriptName
‘ Name of logging file
strLogName = Left(strScript, Len(strScript)-4) & “Log.txt”
‘ Max File Age (in Days).  Files older than this will be deleted.
MaxAge = 1
‘ ************************************************************

set objFSO = createobject(“Scripting.FileSystemObject”)
set objLogFile = objFSO.OpenTextFile(strLogLocation & strLogName, 8, True)

objLogFile.WriteLine(Now & vbTab & “Deleting html files Started”) ‘Write into log file that deleting started

DeleteFiles strFolder,strExtensionsToDelete, maxAge, includeSubFolders

sub DeleteFiles(byval strDirectory,byval strExtensionsToDelete,byval maxAge,includeSubFolders)
DIM objFolder, objSubFolder, objFile
DIM strExt, objLogFile

set objFolder = objFSO.GetFolder(strDirectory)
for each objFile in objFolder.Files
for each strExt in SPLIT(UCASE(strExtensionsToDelete),”,”)
if RIGHT(UCASE(objFile.Path),LEN(strExt)+1) = “.” & strExt then
IF objFile.DateLastModified < (Now – MaxAge) THEN
objFile.Delete
exit for
END IF
end if
next
next
if includeSubFolders = true then ‘ Recursive delete
for each objSubFolder in objFolder.SubFolders
DeleteFiles objSubFolder.Path,strExtensionsToDelete,maxAge, includeSubFolders
next
end if
end sub

objLogFile.WriteLine(Now & vbTab & “Deleting html files completed”) ‘Write into log file that deleting ended

=======================================================================================================================================================

Following was helpful

1)http://www.wisesoft.co.uk/scripts/vbscript_delete_files_by_age_and_file_extension.aspx