Click here to Skip to main content
16,004,782 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have looked through more than 60 pages on the net to try and find the best method to search directories and sub-directories for specified file extensions. I have built an anti-virus but do not wish for it to scan every file type.
Where can I alter this code to allow multiple file extensions to be scanned only? and not every file?

Example:
.exe
.com
.sys

and so forth.

This code is the form of recursion that I am currently using.
VB
Public Sub starting_scan()
            stop_Scan = False
            If Directory.Exists(myscanpath.Text) = True Then
                Scanning = True
                ScanScanner(txtScanningNow.Text)
            Else
                Timer1.Stop()
                MsgBox("The Selected Drive Or Device Is Not Ready Or Has No Scannable Content", MsgBoxStyle.Critical)
                SwitchButton4.Value = False
                Exit Sub
            End If
            Dim drives As String() = Directory.GetLogicalDrives()
            Dim aDrive As String
            For Each aDrive In drives
                myscanpath.Text = aDrive
                ScanScanner(txtScanningNow.Text)
            Next
            stop_Scan = True
            Timer1.Stop()
            Scanning = False
            txtScanningNow.Text = comboBox1.SelectedItem
            SwitchButton4.Value = False
            If lblvirus.Text = 0 Then
                DelVirusInfection.Enabled = False
            Else
                DelVirusInfection.Enabled = True
            End If
    End Sub
    
    Sub ScanScanner(ByVal dir As String)
        MD5Text.Text = MD5Reader.ReadToEnd
        Try
            With My.Computer.FileSystem
                If stop_Scan = True Then
                    Exit Sub
                End If
                For Each file1 In Directory.EnumerateFiles(dir)
                    Dim fs As New FileInfo(file1)
                    ComputerFileHash.Text = getMd5Hash(fs.FullName)
                    labelX13.Text = getSHA1Hash(fs.FullName)
                    If MD5Text.Text.Contains(ComputerFileHash.Text) Then
                        CheckedListBoxControl1.Items.Add(fs.FullName)
                        lblvirus.Text = CheckedListBoxControl1.Items.Count
                    End If
                    getmyfileatr(file1)
                    Dim fileDetail As IO.FileInfo
                    fileDetail = My.Computer.FileSystem.GetFileInfo(file1)
                    lblSizef.Text = fileDetail.Length
                    txtScanningNow.Text = file1
                Next file1
                ' Search child directories.
                For Each folder As Object In .GetDirectories(dir)
                    txtScanningNow.Text = folder
                    ScanScanner(folder)
                Next folder
            End With
        Catch ex As UnauthorizedAccessException
            Return
        End Try
    End Sub

How do I filter this?

Thank you in advance.
Posted
Updated 11-Nov-11 23:35pm
v4

You have not given a search criteria in above criteria.
check the following code to search in directory and sub directories

C#
dim filePaths() as string = System.IO.Directory.GetFiles(@"c:\MyDir\", "*.bmp",SearchOption.AllDirectories)
 
Share this answer
 
Comments
Dale 2012 12-Nov-11 18:08pm    
As I understand the getfiles method only returns one file extension. I am asking how to search for multiple file extensions by altering the code i have so far.
Sergey Alexandrovich Kryukov 12-Nov-11 21:58pm    
I voted 4, because this is not all. Please see my solution, especially the link. There is one problem which is good to know.
--SA
First of all, the solution by Prasad could be correct if not one crazy problem explained here, with the solution: Directory.Get.Files search pattern problem[^].

Dale Seeley wrote:
As I understand the GetFiles method only returns one file extension. I am asking how to search for multiple file extensions by altering the code i have so far.


Is it hard to guess? You need to do the same thing for each extension, one-by-one and unite results obtained from each call to GetFiles.

—SA
 
Share this answer
 
Comments
Dale 2012 14-Nov-11 18:17pm    
Will you be willing to provide an Small solution or example to this?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900