Skip to content

Instantly share code, notes, and snippets.

@antsmartian
Last active August 29, 2015 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save antsmartian/b90039c131545c71b658 to your computer and use it in GitHub Desktop.
Save antsmartian/b90039c131545c71b658 to your computer and use it in GitHub Desktop.
Script to delete the files for the given extensions
Function DeleteFilesWithGivenExtension()
'Change to False, if no need to delete read only file
Const deleteReadOnly = True
Dim directoryLocation
directoryLocation = InputBox("Give the directory location","DirectoryLocation")
Dim fileType
fileType = InputBox("File types to be deleted","File Type")
'Create file object
Set fso = CreateObject("Scripting.FileSystemObject")
'Check the directory given is valid
exists = fso.FolderExists(directoryLocation)
Assert exists, "Please provide valid directory"
'get the folder, later needed for asserting the deletion of files
Set folder = fso.GetFolder(directoryLocation)
Set files = folder.Files
'Make pattern matching string
Dim deletePath
deletePath = directoryLocation + "\*." + fileType
'now delete the files
fso.DeleteFile(deletePath),deleteReadOnly
'now check the files deleted
For each folderIdx in files
If UCase(fso.GetExtensionName(folderIdx.name)) = fileType Then
Assert False, "Delete failed for the file name " + folderIdx
End If
Next
'Now we have tested all the files deleted
DeleteFilesWithGivenExtension = True
End Function
'Custom assert funtion which will be used to throw error @ runtime
sub Assert ( boolValue, strOnFail )
if not boolValue then
Err.Raise vbObjectError + 9999, , strOnFail
end if
end sub
MsgBox(DeleteFilesWithGivenExtension())
'C:\Users\Anto_Belgin\Desktop
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment