VB Script to Delete Files In a Certain Path Having a Certain Extension

https://social.technet.microsoft.com/Forums/scriptcenter/en-US/6d5fb3f5-b553-42ee-8a2d-b86e1c582ad8/vb-script-to-delete-files-of-a-certain-extension

DOWNLOAD ZIP FILE ATTACHED CONTAINING VBS FILE FOR USE ON WINDOWS

Note:  The download file is based on the Technet Script shown below, except that the Echo Confirm Deletion line has been commented-out.  This is a good way to selectively delete those rogue .htaccess files that have been injected into every sub-folder of your website.  Make sure you test this script on a PC on which it will not matter if you happen to mistakenly delete the entire system. USE AT YOUR OWN RISK. BE VERY CAREFUL.  IF YOU DON’T UNDERSTAND THE CODE IN THE SCRIPT, THEN DON’T RUN IT.

OPTION EXPLICIT
DIM strExtensionsToDelete,strFolder
DIM objFSO, MaxAge, IncludeSubFolders

‘ ************************************************************
‘ Setup
‘ ************************************************************

‘ Folder to delete files
strFolder = “D:\test”
‘ Delete files from sub-folders?
includeSubfolders = true
‘ A comma separated list of file extensions
‘ Files with extensions provided in the list below will be deleted
strExtensionsToDelete = “log”
‘ Max File Age (in Days). Files older than this will be deleted.
maxAge = 10

‘ ************************************************************

set objFSO = createobject(“Scripting.FileSystemObject”)

DeleteFiles strFolder,strExtensionsToDelete, maxAge, includeSubFolders

wscript.echo “Finished”

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

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
wscript.echo “Deleting:” & objFile.Path & ” | ” & objFile.DateLastModified
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

Print Friendly, PDF & Email