Advanced File Delete Script

By | October 27, 2006

I saw a Power Shell script recently and it showed how to delete all files recursively for a HD.
Well, I really don't know Power Script and I don't want to change from VBScript.
So, I figured that I should make a script in VBScript that could do the same thing.
This script could be useful to remove .mp3 files or the like from your file server.
WARNING!!! YOU CAN DELETE IMPORTANT SYSTEM FILES IF YOU ARE NOT CAREFUL!!!

So, go ahead and run this on you Domain Controller!!! Just Kidding!!!

———-COPY EVERYTHING BELOW THIS LINE———-

'Script to delete files via name and/or file extension
'The script will search the local HD and delete all files with the same name and/or extension.
'Created by Cheyenne Harden October 27 2006

On Error Resume Next
strComputer = "."
strFileName = inputbox("Enter the name of the file you would like to delete!" & vbCrLf & "Do NOT type in the extension.")
strFileExtension = inputbox("Enter the file extension. Without the " & "'.'")

If strFileName > "" Then
 If strFileExtension > "" Then
  DeleteFileExt()
  
 Else
  DeleteFileName()
 End If

ElseIf strFileExtension > "" Then
 DeleteExtension()
 
Else
 Wscript.Echo "No File Name Or Extension Entered"
 Wscript.Quit

End If

Sub DeleteExtension()

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colFiles = objWMIService.ExecQuery ("Select * From CIM_DataFile Where Extension =  '" & strFileExtension & "'")

For Each objFile in colFiles
 errResults = objFile.Delete
Next
End Sub

Wscript.Echo "Files Deleted"

Sub DeleteFileExt()

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colFiles = objWMIService.ExecQuery ("Select * From CIM_DataFile Where FileName = '" & strFileName & "' and Extension =  '" & strFileExtension & "'")

For Each objFile in colFiles
 errResults = objFile.Delete
Next
End Sub

Sub DeleteFileName()

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colFiles = objWMIService.ExecQuery ("Select * From CIM_DataFile Where FileName = '" & strFileName & "'")

For Each objFile in colFiles
 errResults = objFile.Delete
Next
End Sub

 

 

 

———-COPY EVERYTHING ABOVE THIS LINE———-

 

 

PLEASE MAKE SURE NO WORD WRAPPING IS HAPPENING IN YOUR SCRIPT!!!

To make this script work you will need one things!

1. You will need to have Admin privlidges on the target computer.

This information is provided "AS IS" with no warranties expressed or implied.

Leave a Reply

Your email address will not be published. Required fields are marked *