Support Forums

Full Version: [Source] File Search And Destroy
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Some of the computers here in our place are infected with replicating virus, it spread from one folder to another, not a file infector type of virus.

To ease the pain of removing it one by one, I created this simple tool, you just have to browse a sample file and, set the location for searching, either recursive searching or not, delete file to bin or complete delete.

Screenshot:
[Image: ZNJEi.png]

Code:
Imports System.Threading

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Control.CheckForIllegalCrossThreadCalls = False
    End Sub

    Private Sub BtnBrowse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnBrowse.Click
        If OFD.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
            TextSampleFile.Text = OFD.FileName
            TextFileHash.Text = MD5CalcFile(OFD.FileName)
            BtnScan.Enabled = True
        End If
    End Sub

    Private Sub BtnSetFolder_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnSetFolder.Click
        If FBD.ShowDialog() = DialogResult.OK Then
            TextFolderToScan.Text = FBD.SelectedPath
        End If
    End Sub

    Private Sub BtnScan_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnScan.Click
        Try
            If Not System.IO.File.Exists(TextSampleFile.Text) Then
                MsgBox("The sample file does not exists." & vbNewLine & _
                           "Verify the sample file correctly.", MsgBoxStyle.Critical, "Message")
                If OFD.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
                    TextSampleFile.Text = OFD.FileName
                    TextFileHash.Text = MD5CalcFile(OFD.FileName)
                    BtnScan.Enabled = True
                End If
            Else
                If System.IO.Directory.Exists(TextFolderToScan.Text) = True Then
                    If BtnScan.Text = "Scan" Then
                        ListView1.Items.Clear()
                        TextSampleFile.Enabled = False
                        TxtScanFolder.Text = "Scanning:"
                        t1 = New System.Threading.Thread(AddressOf StartSearch)
                        t1.Start()
                    ElseIf BtnScan.Text = "Abort" Then
                        t1.Abort()
                        TextSampleFile.Enabled = True
                        BtnScan.Text = "Scan"
                        TxtScanFolder.Text = "Status:"
                        TxtScanFolder.Text = "Scan Aborted. " & "Match found: " & Filescount.Text & " files"
                        MsgBox("Scan Aborted", MsgBoxStyle.Critical, "Message")
                    End If
                Else
                    MsgBox("The folder does not exists." & vbNewLine & _
                           "Verify the folder correctly.", MsgBoxStyle.Critical, "Message")
                    If FBD.ShowDialog() = DialogResult.OK Then
                        TextFolderToScan.Text = FBD.SelectedPath
                    End If
                    Exit Sub
                End If
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Dim t1 As New System.Threading.Thread(AddressOf StartSearch)
    Private Sub StartSearch()
        If ChkRecursive.Checked = True Then
            Filescount.Text = 0
            BtnScan.Text = "Abort"
            RecursiveSearch(TextFolderToScan.Text, "*.*", True)
        ElseIf ChkRecursive.Checked = False Then
            Filescount.Text = 0
            BtnScan.Text = "Abort"
            RecursiveSearch(TextFolderToScan.Text, "*.*", False)
        End If
        TextSampleFile.Enabled = True
        BtnScan.Text = "Scan"
        TxtScanFolder.Text = "Status:"
        TxtScanFolder.Text = "Scan Complete. " & "Match found: " & Filescount.Text & " files"
        MsgBox("Match found: " & Filescount.Text & " files", MsgBoxStyle.Information, "Scan Complete")
    End Sub

    Private Sub BtnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnClose.Click
        End
    End Sub

    Private Sub RecursiveSearch(ByVal path As String, ByVal searchpattern As String, ByVal Recursive As Boolean)
        Try
            Dim dirInfo As New IO.DirectoryInfo(path)
            Dim fileObject As System.IO.FileSystemInfo
            'If recursive true
            If Recursive Then
                For Each fileObject In dirInfo.GetDirectories
                    RecursiveSearch(fileObject.FullName, searchpattern, Recursive)
                Next
            End If
            'If not recursive
            For Each fileObject In dirInfo.GetFiles
                Dim ObjFSO As Object = CreateObject("Scripting.FileSystemObject")
                Dim objFile = ObjFSO.GetFile(fileObject.FullName)
                TxtScanFolder.Text = objFile.ShortPath

                If My.Computer.FileSystem.GetFileInfo(TextSampleFile.Text).Length = My.Computer.FileSystem.GetFileInfo(fileObject.FullName).Length Then
                    If MD5CalcFile(fileObject.FullName) = TextFileHash.Text Then
                        ' Check for normal files
                        Filescount.Text = Filescount.Text + 1
                        Dim str(1) As String
                        Dim itm As ListViewItem
                        str(0) = fileObject.FullName
                        itm = New ListViewItem(str)
                        ListView1.Items.Add(itm)
                    End If
                End If
                objFile = Nothing
            Next
        Catch ex As Exception
            Exit Sub
        End Try
    End Sub

    Private Sub BtnRemoveFiles_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnRemoveFiles.Click
        Dim title = "Confirm file delete"
        Dim style = MsgBoxStyle.OkCancel
        Dim msg = "Are you sure you want to perform this action?"
        Dim response = MsgBox(msg, style, title)

        If response = MsgBoxResult.Ok Then
            For Each items As ListViewItem In ListView1.Items
                If ChkRecycleBin.Checked = True Then
                    SetAttr(items.Text, vbNormal)
                    My.Computer.FileSystem.DeleteFile(items.Text, _
                    FileIO.UIOption.OnlyErrorDialogs, FileIO.RecycleOption.SendToRecycleBin)
                Else
                    SetAttr(items.Text, vbNormal)
                    My.Computer.FileSystem.DeleteFile(items.Text, _
                    FileIO.UIOption.OnlyErrorDialogs, FileIO.RecycleOption.DeletePermanently)
                End If
            Next
            ListView1.Items.Clear()
            Dim Deletetype
            If ChkRecycleBin.Checked = True Then
                Deletetype = "moved to Recycle Bin."
            Else
                Deletetype = "removed permanently."
            End If
            MsgBox("All Match File(s) successfully " & Deletetype, MsgBoxStyle.Information, "Message")
        Else
            Exit Sub
        End If
    End Sub

    Private Sub BtnAbout_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnAbou.Click

    End Sub

    ' ****************************************************************
    ' Get MD5 hash of a file
    Public Function MD5CalcFile(ByVal filepath As String) As String
        ' open file (as read-only)
        Using reader As New System.IO.FileStream(filepath, IO.FileMode.Open, IO.FileAccess.Read)
            Using md5 As New System.Security.Cryptography.MD5CryptoServiceProvider
                ' hash contents of this stream
                Dim hash() As Byte = md5.ComputeHash(reader)
                ' return formatted hash
                Return ByteArrayToString(hash)
            End Using
        End Using
    End Function

    ' Utility function to convert a byte array into a hex string
    Private Function ByteArrayToString(ByVal arrInput() As Byte) As String
        Dim sb As New System.Text.StringBuilder(arrInput.Length * 2)
        For i As Integer = 0 To arrInput.Length - 1
            sb.Append(arrInput(i).ToString("X2"))
        Next
        Return sb.ToString().ToLower
    End Function
    ' End
    ' ****************************************************************

End Class

I hope this helps.