Recent Posts

Thursday, September 23, 2010

How to delete file in folder and sub folders?

I came across a virus that created some files in all my computer folder and sub folders. I luckly removed the virus using virus scan but some how could not delete the files via virus scanner. It was impossible for me to delete all the files manually.

Therefore i thought of writing a small visual basic script that cleans up all the files specified from windows folders and subfolders.

The vb .net code that deletes specified files from folders and sub folders is:

Private Sub DeleteFiles(ByVal Path As String)

Dim Directories As New IO.DirectoryInfo(Path)
Dim Directory As IO.DirectoryInfo

Dim strFile As String
Dim File As IO.FileInfo

Dim strErr1 As String = "index.php"
Dim strErr2 As String = "index.html"
Dim strErr3 As String = "index.htm"
Dim strErr4 As String = "index.cfm"
Dim strErr5 As String = "index.asp"
Dim strErr6 As String = "default.php"
Dim strErr7 As String = "default.html"
Dim strErr8 As String = "default.htm"
Dim strErr9 As String = "default.cfm"
Dim strErr10 As String = "default.asp"

For Each Directory In Directories.GetDirectories
For Each File In Directory.GetFiles
strFile = File.Name
If strFile = strErr1 Or strFile = strErr2 Then
File.Delete()
ElseIf strFile = strErr2 Then
File.Delete()
ElseIf strFile = strErr3 Then
File.Delete()
ElseIf strFile = strErr4 Then
File.Delete()
ElseIf strFile = strErr5 Then
File.Delete()
ElseIf strFile = strErr6 Then
File.Delete()
ElseIf strFile = strErr7 Then
File.Delete()
ElseIf strFile = strErr8 Then
File.Delete()
ElseIf strFile = strErr9 Then
File.Delete()
ElseIf strFile = strErr10 Then
File.Delete()
End If
Next
'ListBox1.Items.Add(Directory.Name)
If Directory.GetDirectories.Length > 0 Then
DeleteFiles(Directory.FullName)
End If
Next
End Sub


Read more!