So I have some content servers that require (when we push some types of code) a full flush of 10's of thousands of files that reside on said servers. I did some research on the net hoping to find that someone else had been though the pain I just did, to no avail. I found some that were similar to what I needed but I had to add more and make some changes. The only ones that I found were extension only, but my need was for a wildcard in the middle of the string and to ID all files that I wanted to delete and to only delete the files that were a said ext. So as you can see from the script I needed ‘data’ & wildcard & “.” & ext.
I figured that I had to do it, I might as well help someone else out with my pain. Please feel free to let me know if there is an easier way to do this, or a better one for that matter.
' ====================================================================================
' TITLE: delete_files_by_extension.vbs
' AUTHOR: ME
' HOW TO USE: Run it from computer/server that has access to content servers.
' ====================================================================================
OPTION EXPLICIT
DIM strExtensionsToDelete,strFolder
DIM objFSO
' ====================================================================================
' TITLE: Input and MsgBox
' PURPOSE: Runs and input data.
' ====================================================================================
DIM Title, Text1
DIM Input, result
Title = "File removal script" 'Script Title
Input = inputbox("Enter Directory To Scan", Title) 'InputBox Tree
If Input = "" then
objExplorer.Document.Body.InnerHTML = "You must enter a valid directory"
wscript.quit
else
end if
Text1 = "You selected " & Input & " is this correct?" 'Misc Text - Edit as needed
result= MsgBox(Text1, vbInformation + vbYesNo, Title) 'Results data
If result =vbYes Then 'Result = Yes
MsgBox ("Application Started, it will finish soon.") 'MsgBox on vbYes
Else
MsgBox "You fail, Program Terminated" 'MsgBox on vbNo
End if
strFolder = Input
strExtensionsToDelete = "html" 'This is where you add your file extensions, you can add more by adding ',' e.g 'html, mp3, rar, mov' etc.
set objFSO = createobject("Scripting.FileSystemObject")
RecursiveDeleteByExtension strFolder,strExtensionsToDelete
' ====================================================================================
' TITLE: Sub
' PURPOSE: Run and choose a folder to analyze.
' ====================================================================================
sub RecursiveDeleteByExtension(byval strDirectory,strExtensionsToDelete)
DIM objFolder, objSubFolder, objFile
DIM strExt
DIM strFileName
set objFolder = objFSO.GetFolder(strDirectory)
for each objFile in objFolder.Files
for each strExt in SPLIT(UCASE(strExtensionsToDelete),",")
strFileName = objFSO.GetFileName(objFile.Path)
IF RIGHT(UCASE(strFileName),LEN(strExt)+1) = "." & strExt then 'This is where you tell it to grab the extension
IF LEN(strFileName) >= LEN(strExt)+15 THEN 'Change the LEN to whatever number you need
IF LEFT(UCASE(strFileName), 14) = "15_CHARS_WORTH_OF_DATA" THEN 'See line above
objFile.Delete
exit for
END IF
END IF
END IF
next
next
for each objSubFolder in objFolder.SubFolders 'This is the call to do the same in Sub Folders
RecursiveDeleteByExtension objSubFolder.Path,strExtensionsToDelete 'Recursive
next
end sub