?
Solved

reading through directories and files in .NET

Posted on 2008-10-29
7
Medium Priority
?
251 Views
Last Modified: 2013-12-17
I have a program that is searching through a specified directory and its subdirectories.
I want to list for this directory and each subdirectory the names of all files that
contain a specific word, lets say for example: "duck".  I will then add that file to a textbox display.

The code snippet I have, I thought should work...but I am not reading in any files.  Is it how I am cheching for 'duck"?    if (file.Contains("duck") )

Thanks.
private void listFiles(string a_directory)
        {
            // get a list of all the files directories within the directory.
            string[] files = Directory.GetFiles(a_directory);
            string[] directories = Directory.GetDirectories(a_directory);
 
            foreach (string file in files)
            {
                // display only files that have gteater assecced time
                if (file.Contains("duck") )
                {
                    txt_results.Text += file + "\r\n";
                }
 
            }
            // go through all the directories and do the same thing.
            foreach (string dir in directories)
            {
                if (dir.Contains("duck"))
                {
                    txt_results.Text += dir + "\r\n";
                }
 
                // call recursivly to hit all directories
                listFiles(dir);
            }
        
        }

Open in new window

0
Comment
Question by:wizkid2332
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
7 Comments
 

Author Comment

by:wizkid2332
ID: 22835077
actually--I realized I was not reading through the actual files...just cheking the names of the files--but I added a hash function and still can not get it to work...
 private Hashtable hashTbl = new Hashtable();
 
 
            // generate all of the directories and files.
            listFiles(directory);
        }
        private void listFiles(string a_directory)
        {
            // get a list of all the files directories within the directory.
            string[] files = Directory.GetFiles(a_directory);
            string[] directories = Directory.GetDirectories(a_directory);
 
            foreach (string file in files)
            {
              
                    // read and store data
                     readData(file);
                     // display results
                     display();
          
 
            }
            // go through all the directories and do the same thing.
            foreach (string dir in directories)
            {
 
                // call recursivly to hit all directories
                listFiles(dir);
            }
        
        }
 
        private void readData(string path)
        {
            try
            {
                // open file for reading
                StreamReader stream = new StreamReader(path);
 
                // * means give me 1 or more blanks after delims
                string pattern = @"[ ,:.;{}()&\n\r\t\s ] *";
 
                Regex rx = new Regex(pattern);
 
                // read the file to the end
                string text = stream.ReadToEnd();
 
                // close file after reading through
                stream.Close();
 
                // split using the patern
                string[] words = rx.Split(text);
 
                foreach (string wrd in words)
                {
                    if (wrd == "duck")
                    {
                    // add words to the hash table
                        hashTbl.Add(path, "1");
                    }
 
                }
 
            }
            catch (IOException)
            {
                // display error if read fails
                MessageBox.Show("Problem reading from the file " + path);
 
            }
        }
 
        private void display()
        {
            foreach (DictionaryEntry de in hashTbl)
            {
                // display information to the output box
                txt_results.Text += "Word: " + de.Key  + " times." + "\r\n";
 
            }
 
        }

Open in new window

0
 
LVL 4

Expert Comment

by:novynov
ID: 22835162
I hope I'm not asking stupid or insulting questions, but I just want to be sure I understand what you are trying to accomplish.

- Is this being called from a WinForms application or a Web app? If it is being called from a web app, I'm just confirming that you know that the directory traversal will be done on the server, not on the client (i.e. browser). So, if the server has no files with that name, you will get an empty list.

I ran your function "as-is" within a WinForms app, and it worked fine...even with some mixed case file names.

Have you tried matching for a different pattern in the filename? Does this work?
0
 
LVL 4

Expert Comment

by:novynov
ID: 22835196
Ooops...my other question was to confirm that you were looking to match the filename not the file contents. It looks like you are wanting to match the contents.

Are you searching text files, binary files or both?
0
Learn by Doing. Anytime. Anywhere.

Do you like to learn by doing?
Our labs and exercises give you the chance to do just that: Learn by performing actions on real environments.

Hands-on, scenario-based labs give you experience on real environments provided by us so you don't have to worry about breaking anything.

 

Author Comment

by:wizkid2332
ID: 22835514
I am searching text files.  It is a WinForms application. thanks!
0
 

Author Comment

by:wizkid2332
ID: 22835659
but--on my second post, I am trying to search the contents, but I am not getting any results in my display...
0
 
LVL 9

Expert Comment

by:DrAske
ID: 22836054
Change the following
                   if (wrd == "duck")
To something like this:
                  if(wrd.Contains("duck"))
0
 
LVL 4

Accepted Solution

by:
novynov earned 600 total points
ID: 22836145
I'm running your code now and am getting matches...as well as display in the textbox.

One thing I noticed is that you are calling display() after each file is processed. This has the effect of adding some items multiple times to the textbox. Is this what you intended? This isn't likely the problem you are having...I just noticed it.

Can you attach one of the text files you believe should be matching, so I can try it with your data?

0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question