Click here to Skip to main content
15,172,754 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Ok to start I would like to have the user select a root directory from a combobox (Example, "C:\"). Once that happens the start button is enabled and the text file with my loaded file names is displayed in a textbox. The user clicks start to scan the C drive and the label shows the directory and file name from the text file that the computer is looking for.........

I am no good with recursion and have been trying to learn it for days. I get the idea of recursion and know it may be the most efficient way to scan a computers folders and sub folders but what I truly need is a full example word for word and what object controls what line of code...... Please I am desperate to understand recursion and need help to make my scanner search the entire directory and not just one level.
Posted
Updated 17-Oct-10 22:37pm
v3
Comments
Gurpreet_Fusion 17-Dec-12 1:14am
   
While i am scaning the C or D directory i am getting a access denied error.. How can i solve this error

While I recommend that you try to learn recursion, actually it's not needed here. The GetFiles method in the IO namespace will let you find all files in a given directory AND it's subdirectories.

Something like this:

VB
Imports System.IO

Dim dir As New DirectoryInfo("C:\")
For Each foundFile As FileInfo In dir.GetFiles("*.*", SearchOption.AllDirectories)
    'Do Magic here

Next foundFile


In this case, the AllDirectories flag specifies that it should loop through all subdirectories recursively...
   
v4
Comments
Dale 2012 18-Oct-10 4:02am
   
Thank you so much for that....... I thought my only way out of this one would be recrusion. I am in need of some guidance when placing this code into effect.... For example.... is your get file code needed in the form load? or maybe the start button click event? please advise or if possible provide a code with the given code to some form objects.....
Johnny J. 18-Oct-10 4:09am
   
In your scenario above, you would put it in the buttons click event. My recommendation is that you disable the button while the scanning is running and enable it again when the scanning is done... Just to make sure the user doesn't click it and start another scan while one scan is already in progress.
Dale 2012 18-Oct-10 4:29am
   
please you sound very well versed in this topic and it means everything to my project to do this so if i might add one last request

would you please spoon feed me on this one?? :(

I have a combobox that reads all root dirs (combobox1.items.addrange(system.io.directory.getlogicaldrives)
then the user clicks a root dir and the start scan button is enabled
also at this point the text file with the names of the files i wish to find is displayed in the textbox.
I dont know what else to add here please help
Dale 2012 18-Oct-10 4:31am
   
The get file method seems very promising but I need the search to check all directories with the names in my text file...
any suggestions or code to help me?
Johnny J. 18-Oct-10 4:32am
   
Sorry, Dale, but I don't quite understand what it is you're tyring to do and what role the textbox with the file name plays in the scenario...
Dale 2012 18-Oct-10 4:36am
   
ok ill try to describe this from the users view

first
the user chooses what root directory to scan from in the combobox
next
the user presses start and the program searches all subfolders for the names in my text file
Dale 2012 18-Oct-10 4:39am
   
its a definition virus scanner but at the moment it only scans one level

if I select C:\ it only scans that folder and nothing inside it or inside of those folders
and would like it to scan everything in the root dir until my text file is EOF
Johnny J. 18-Oct-10 4:39am
   
OK, let me see if I understand this correctly:

1) All drives are shown in a combobox
2) The user selects a drive and depending on what drive it is, you load a text file containing a number of filenames and present it to your user? Is it ONLY the file names or do they contain a path as well? If the path is not specified, does it matter what directory the file is found in? What if the file is found in more than one directory?
3) You hit the scan button and scan the drive for the files specified. What do you wat to do with the result of the scan?
Dale 2012 18-Oct-10 4:45am
   
You are correct all drives are shown in a combobox
2) user selects a drive and no matter what drive is chosen the text file is used to supply the scan with the name the program is scanning for

if the file is found Id like it to report it to a text or list box to then delete the infected file
Dale 2012 18-Oct-10 4:47am
   
the problem is that I want all subfolders to be checked for the given drive or root dir and how to use the method in a combobox and start button so that the user does not need to specify the exact location of the virus as that could be very time consuming
Dale 2012 18-Oct-10 4:48am
   
I would like the chosen root dir such as C:\ in the combobox to search all subfolders to find the file names listed in my text file
Dale 2012 18-Oct-10 4:53am
   
I think I need to re phrase my question the best i can

1) the user selects a root dir in the combobox (example....C:\)
2) the text file with the virus names is loaded into a textbox for the user to see
3) start scan is enabled and the search goes into every folder in the root dir untill the text file is done
4) the matched search results from the text file are shown in a text or listbox for the user to review and delete
Dale 2012 18-Oct-10 5:00am
   
The third statement here I think should end when the folders end and not the text file but again I have searched everywhere to no avail you may be my only hope in this matter
Johnny J. 18-Oct-10 5:00am
   
Try something like this:

Private Sub btnScan_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnScan.Click
btnScan.Enabled = False

Dim searchForTheseFiles() As String = txtFileList.Text.ToUpper.Split(Environment.NewLine)
Dim fileList As New List(Of String)(searchForTheseFiles)
Dim myDir As New DirectoryInfo(cboDriveList.Text)

For Each foundFile As FileInfo In myDir.GetFiles("*.*", SearchOption.AllDirectories)
If fileList.Contains(foundFile.Name.ToUpper) Then
MsgBox("AAAAAHHHHH!!!! VIRUS FOUND!!!")
txtVirusesFound.Text &= Environment.NewLine & foundFile.FullName
Try
foundFile.Delete()
Catch ex As Exception
MsgBox("Couldn't delete infected file!")
txtVirusesFound.Text &= Environment.NewLine & "Couldn't delete infected file: " & foundFile.FullName
End Try
End If
Next foundFile

btnScan.Enabled = True
End Sub

I don't want to be rude, but if you need more help the you'd better hire a programmer. I and most of the people here normally get paid to write code...
Dale 2012 18-Oct-10 5:29am
   
last of all can you clear up words such as.....txtfilelist.text? and cbodrivelist.text? and foundfile.name? and txtvirusesfound? I hate to be rude to you by asking for so much but I am in my learning stage
Johnny J. 18-Oct-10 5:33am
   
I thought the names self explanatory, but here you go:
txtFileList = the name of the textbox where you load your textfile containing the file names
cboDriveList = the name of your combobox containing the drive letters
foundFile = a variable used to hold the file info of the currently found file (I suggest you read up on the System.IO.FileInfo and System.IO.DirectoryInfo classes to get acquainted with them)
foundFile.Name = the name of the currently found file without path
Johnny J. 18-Oct-10 5:34am
   
txtvirusesfound = The name of a textbox (or similar) you use to show the results of the scan - In this case, I write the name of the file that is found, and I write a message if it's impossible to delete the file. You might want to present it in a different way
Dale 2012 18-Oct-10 5:36am
   
I can pay you once my program takes off but I know right now your trying to do me a huge favor and I thank you for what thats worth and if you give me just a sec i will test the code and let you know how it goes......I understand your time is worth money and if I get this going the way it is supposed to I will surly keep you in mind for a payment somehow no lie.....
Dale 2012 18-Oct-10 5:46am
   
ok so this is what i have so far but cannot know what the found file is

Public Class Form1
Private Sub btnScan_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnscan.Click
btnscan.Enabled = False
Dim searchForTheseFiles() As String = txtFileList.Text.ToUpper.Split(Environment.NewLine)
Dim fileList As New List(Of String)(searchForTheseFiles)
Dim myDir As New DirectoryInfo(cboDriveList.Text)
For Each foundFile As FileInfo In myDir.GetFiles("*.*", SearchOption.AllDirectories)

Next
If fileList.Contains(foundFile.Name.ToUpper) Then
MsgBox("AAAAAHHHHH!!!! VIRUS FOUND!!!")

textbox1.Text &= Environment.NewLine & foundFile.FullName
Try foundFile.Delete()
Catch ex As Exception
MsgBox("Couldn't delete infected file!")
textbox1.Text &= Environment.NewLine & "Couldn't delete infected file: " & foundFile.FullName
End Try
End If
Next
foundFile()
btnscan.Enabled = True
End Sub
End Class
Dale 2012 18-Oct-10 5:47am
   
how will I name or know what virus is found when?

or is found file a object?
Johnny J. 18-Oct-10 5:48am
   
Exactly - It's an object. That's why I came with the suggestion before: "I suggest you read up on the System.IO.FileInfo and System.IO.DirectoryInfo classes to get acquainted with them"

They are very useful, and you will definitely need to know about them for this project.
Dale 2012 18-Oct-10 5:52am
   
Ok it looks as though it works but it says Access to the path 'C:\Documents and Settings\' is denied. and the scan stops........
Dale 2012 18-Oct-10 5:55am
   
can i skip and resume to the next subfolder somehow?
Johnny J. 18-Oct-10 5:56am
   
Well, access to certain folders (including the "C:\" root) is not always allowed. You will need to adjust the security settings for your application and request full access to the file system.

That's a matter I'm not able to help you with - unfortunately, I'm not an expert on that, so you might want to open a new thread for that.
Dale 2012 18-Oct-10 5:56am
   
actualy I am very sorry to have asked you all this and I appriciate everything you have told me so as not to be rude if you choose not to go on I will understand....
Dale 2012 18-Oct-10 5:58am
   
thank you very much you have added allot more insight to my problems then I have been able to gather in days thanks again I will rate you a 5
Johnny J. 18-Oct-10 5:58am
   
Skipping the subfolder would not be the correct thing to do. What's the use of a virus scanner that doesn't scan all the files?
Dale 2012 18-Oct-10 5:59am
   
very true thanks again
An example (written just for you!): "Directory.GetFiles Method (String)"[^] at MSDN.
:-)
   
Comments
Dale 2012 18-Oct-10 4:03am
   
this will help greatly thank you!! but if possible would you write a search function that will scan for names with a text file..... the names i wish to find are in the text file.....definition scanner
Johnny J. 18-Oct-10 4:10am
   
Oh yes, can't you do that c? You can put it on MSDN with the other article... ;-)
Dale 2012 18-Oct-10 4:33am
   
please johnny will you provide a code that searches all subfolders when the user selects a root dir from a combobox? my scanner is a definition type scanner that checks for the names using a text file

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