Click here to Skip to main content
15,889,877 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am not having any problem with this code and it is doing what it should but I feel that it is not checking the system for the names in my text file. What is ment by "Put your path here?" and how can I display the files that are being searched through in a label as opposed to a list or text box?. the scan also seems to not exit.... and my stop and delete buttons no longer work once this code is in place... any ideas or can someone rewrite this? please help







Imports System.IOPublic Class Form1
    
Private Sub searchButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles searchButton.Click        
Dim direc As New DirectoryInfo("C:\");Put your path here. 
If direc.Exists = False Then
            MsgBox("The path does not exist. Please specify a correct path")            
Exit Sub
        End If
        ListBox1.Items.Add("Folders are : ")        Application.DoEvents()        
RepeatDir(direc)    
End Sub
    Function RepeatDir(ByVal drr As DirectoryInfo)        
Dim directoryListing() As DirectoryInfo = drr.GetDirectories()        For Each fls As DirectoryInfo In directoryListing            
Try
                ListBox1.Items.Add(fls.FullName)                RepeatDir(fls)                
ListBox1.Items.Add("Checking for Files:")                RepeatFiles(fls)            
Catch ex As Exception                
ListBox1.Items.Add("Unable to access directory:" & fls.FullName)            
End Try
            Application.DoEvents()       
 Next
    End Function
    Function RepeatFiles(ByVal dirs As DirectoryInfo)       
 Dim fileListing() As FileInfo = dirs.GetFiles()        
If fileListing.Length > 0 Then
            For Each fl As FileInfo In fileListing                ListBox1.Items.Add(fl.FullName)            
Next
        Else
            ListBox1.Items.Add("No files are present in this folder " & dirs.Name)       
 End If
        Application.DoEvents()   
 End Function
End Class
Posted
Updated 25-Oct-10 1:27am
v3
Comments
Keith Barrow 25-Oct-10 2:38am    
Edits: Removed "PLEASE HELP" from the title, this will stop people from helping, and CAPITALS are considered shouting. Code wrapped in "pre" tags.
I suggest you format your code properly (indent etc. Otherwise, people will just complain about that rather than helping you with you problem, as it is difficult to read poorly formatted code.

You state that you are not having any problems with this code but then imply that you are. From the rest of your comments it would seem that this is someone else's code that you are trying to adapt in a project of your own. Try being a bit more specific about which parts are not working as you expect. In the line:
Dim direc As New DirectoryInfo("C:\");Put your path here. 

it seems fairly clear that this is a comment from the original author that you should replace the string "C:\" with the path that you wish to use as the start of your search.
 
Share this answer
 
Comments
Dalek Dave 25-Oct-10 5:49am    
To be fair, don't we all 'adopt, adapt and improve' code snippets we find that may prove useful?
Rod Kemp 25-Oct-10 6:28am    
Actually it's my adaptation of the original authors code http://www.codeproject.com/Questions/121074/Calling-a-module-assigning-a-valid-path.aspx
I put that comment in so he would know where to put the path he wanted to check, which obviously didn't work.
Richard MacCutchan 25-Oct-10 6:34am    
To DD: Absolutely yes, I do it all the time, but in this case the OP did not mention it with his first post, I had to deduce it from the comments.

To Rod: Can you offer any more advice to the OP?
Tarun.K.S 25-Oct-10 6:42am    
thats my code! the function RepeatFiles and RepeatDir was given by me to Dale!
Rod Kemp 25-Oct-10 7:24am    
To Richard: Have done.

To Tarun: Yep it sure is, I just changed it a little to work in a WinForm app with some additional changes to suit my preferences and to skip directories that a user doesn't have access to.
OK now I'm leaving the delete functionality out as an exercise for you.
When you click the stop button this code doesn't restart from the place it stopped at if you click on the search button again but from the beginning.

You will need on your form;
A button called searchButton
A button called stopButton
A TextBox called searchPath
A button called searchPathSelect
a Label called currentPath
and a listbox called ListBox1

************** All Code Updated *****************

VB
Imports System.IO
Public Class Form1

    Private Sub searchButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles searchButton.Click
        If searchPath.Text <> String.Empty Then
            Dim direc As New DirectoryInfo(searchPath.Text)
            If direc.Exists = False Then
                MsgBox("The path does not exist. Please specify a correct path")
            Else
                searchButton.Enabled = False
                stopButton.Enabled = True
                Application.DoEvents()

                RepeatDir(direc)
                RepeatFiles(direc)

                MessageBox.Show("Scan Complete")
            End If
        Else
            MessageBox.Show("Select a path to begin searching from.")
        End If
    End Sub

    Private Sub stopButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles stopButton.Click
        searchButton.Enabled = True
        stopButton.Enabled = False
    End Sub

    Private Sub searchPathSelect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles searchPathSelect.Click
        Dim browserDialog As New FolderBrowserDialog()
        Dim dialogResult As DialogResult = browserDialog.ShowDialog()
        If dialogResult = Windows.Forms.DialogResult.OK Then
            searchPath.Text = browserDialog.SelectedPath
        End If
    End Sub

    Function RepeatDir(ByVal drr As DirectoryInfo)
        Dim directoryListing() As DirectoryInfo = drr.GetDirectories()
        For Each fls As DirectoryInfo In directoryListing
            Try
                If searchButton.Enabled = False Then
                    RepeatDir(fls)
                    RepeatFiles(fls)
                End If
            Catch ex As Exception
                ListBox1.Items.Add(ex.Message)
            End Try
        Next
    End Function

    Function RepeatFiles(ByVal dirs As DirectoryInfo)
        Dim fileListing() As FileInfo = dirs.GetFiles()
        If fileListing.Length > 0 Then
            For Each fl As FileInfo In fileListing
                If searchButton.Enabled = False Then
                    currentPath.Text = fl.FullName
                    Application.DoEvents()
                    Dim currentFileName As String = fl.Name
                    Dim fileExists As Boolean = My.Resources.VirusList.Contains(currentFileName)
                    If fileExists = True Then
                        If MessageBox.Show("File found do you want to delete it?", "File Found", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes Then
                            ListBox1.Items.Add("Removing File: " & fl.Name)
                            fl.Delete()
                        End If
                    End If
                End If
            Next
        End If
    End Function
End Class
 
Share this answer
 
v3
Comments
Dale 2012 25-Oct-10 21:55pm    
Thank you very much guys and yes it is your code of which I would never else wise say so I will try what you have stated here to try and get the scan to look for the names in the text file I have in my.resources.viruslist

Also when I applied the given code my stop button and delete button no longer work along with the textbox that scrolls through the virus names provided in the resources. besides that Im not sure how much more you would like to know in order to help me if you decide to still help it would surely be appriciated. even off or close to the topic info is needed as I am still learning allot about .......well allot.... lol
Dale 2012 25-Oct-10 23:21pm    
This is everything I needed since I have started asking questions but it forgets the main portion of what I need which is the text file with the virus names. It is scanning like a charm but how can it use the text file in this code so that the scanner is checking the computer against the names in that text file?
Dale 2012 25-Oct-10 23:21pm    
btw you all get 5 out of 5 for sure :)
Rod Kemp 25-Oct-10 23:30pm    
In the RepeatFiles method is where you need to check the files that it is scanning through against your text file.
Really any where after "currentPath.Text = fl.FullName", so long as it stays in the IF block, is where you would check the file name against your text file, if you use "fl.Name" you will get just the name of the file without the path.
Dale 2012 26-Oct-10 1:34am    
If I need to keep my text file (my.resources.viruslist) In the If block for the computer to check the text file do I put the code (my.resources.viruslist) like this? "my.resources.viruslist.text = fl.FullName"? or how would I write the code to place it as you have said?

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