7 Replies - 1404 Views - Last Post: 10 December 2010 - 08:28 AM Rate Topic: -----

#1 harryad2010  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 10-December 10

C# file search problem

Posted 10 December 2010 - 04:51 AM

Hello,
last year i started programming on my own, so far all i have been is simple, but i hit a problem with the code to look for a file in a location.

what i want the code to do is give all the files that meet the requirements in the given directory and subdirectory's.
but all i get is the results of the main folder and the first subdirectory. What i am doing wrong.

using System;
using System.IO;
public class FileSearch
{
    public static void Main()

    {
        Console.WriteLine("input name or part of the name.");
        string userinput1 = Console.ReadLine();
        Console.WriteLine("input location to search");
        string userinput2 = Console.ReadLine();
        String searchName = userinput1;
        DirectoryInfo myDir = new DirectoryInfo(@userinput2);
        SearchDirectories(myDir, searchName);
    }

    public static void SearchDirectories(DirectoryInfo dir, String target)
    {
        FileInfo[] files = dir.GetFiles();
        foreach (FileInfo file in files)
        {
            if (file.Name.IndexOf(target) > -1)
            {
                Console.WriteLine(file.Name);
            }
        }

        DirectoryInfo[] dirs = dir.GetDirectories();
        foreach (DirectoryInfo subDir in dirs)
        {
            SearchDirectories(subDir, target);
        }
            Console.WriteLine("if you want to exit the program type close.");
            string close = Console.ReadLine();
            if (close.ToLower() == "close") { return; }
            else { Main();
            Console.Clear();
                    }
    }
}



Is This A Good Question/Topic? 0
  • +

Replies To: C# file search problem

#2 mavarazo  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 37
  • View blog
  • Posts: 182
  • Joined: 25-October 10

Re: C# file search problem

Posted 10 December 2010 - 05:34 AM

Why do you have the console handling in the recursive method?
I would place it in the Main-Method.
Was This Post Helpful? 0
  • +
  • -

#3 Zunera  Icon User is offline

  • D.I.C Head

Reputation: 28
  • View blog
  • Posts: 74
  • Joined: 07-December 10

Re: C# file search problem

Posted 10 December 2010 - 06:42 AM

The SearchDirectories functionality already exists within the .NET framework:
public static void Main()
{
  ...
  System.IO.DirectoryInfo myDir = new System.IO.DirectoryInfo(@userinput2);
  System.IO.FileInfo[] result = myDir.GetFiles("*" + searchName + "*", System.IO.SearchOption.AllDirectories);
  foreach (System.IO.FileInfo fileInfo in result) Console.WriteLine(fileInfo.FullName);
  ...
}


Was This Post Helpful? 1
  • +
  • -

#4 harryad2010  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 10-December 10

Re: C# file search problem

Posted 10 December 2010 - 07:14 AM

View PostZunera, on 10 December 2010 - 05:42 AM, said:

The SearchDirectories functionality already exists within the .NET framework:
public static void Main()
{
  ...
  System.IO.DirectoryInfo myDir = new System.IO.DirectoryInfo(@userinput2);
  System.IO.FileInfo[] result = myDir.GetFiles("*" + searchName + "*", System.IO.SearchOption.AllDirectories);
  foreach (System.IO.FileInfo fileInfo in result) Console.WriteLine(fileInfo.FullName);
  ...
}



Thanks alot :) got it working now.
Was This Post Helpful? 0
  • +
  • -

#5 CodeGrappler  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 41
  • View blog
  • Posts: 120
  • Joined: 29-November 10

Re: C# file search problem

Posted 10 December 2010 - 07:24 AM

*EDIT* I see you posted while I was writing this up. I do realize the .NET framework has built in functionality to accomplish this however wheres the fun in not learning how to do it yourself? :)

You need to remove the console stuff from the SearchDirectories function. You should never ever call Main anywhere ever, It's just bad joojoo.

The real problem is since you are asking for input and calling Main in your recursive function whenever those foreach's finish for each directory. If you were to type close enough times you would eventually see the final output of your program.

This is my proposed solution that keeps your intended functionality in the order you had it.

using System;
using System.IO;
public class FileSearch
{
    public static void Main()
    {
        while(true)
        {
            Console.WriteLine("input name or part of the name.");
            string userinput1 = Console.ReadLine();
            Console.WriteLine("input location to search");
            string userinput2 = Console.ReadLine();
            String searchName = userinput1;
            DirectoryInfo myDir = new DirectoryInfo(@userinput2);
            SearchDirectories(myDir, searchName);

            Console.WriteLine("if you want to exit the program type close.");
            string close = Console.ReadLine();
            if (close.ToLower() == "close")
                break;
            
            Console.Clear();
        }
    }

    public static void SearchDirectories(DirectoryInfo dir, String target)
    {
        FileInfo[] files = dir.GetFiles();
        foreach (FileInfo file in files)
        {
            if (file.Name.IndexOf(target) > -1)
            {
                Console.WriteLine(file.Name);
            }
        }

        DirectoryInfo[] dirs = dir.GetDirectories();
        foreach (DirectoryInfo subDir in dirs)
        {
            SearchDirectories(subDir, target);
        }
    }
}


This post has been edited by CodeGrappler: 10 December 2010 - 07:25 AM

Was This Post Helpful? 1
  • +
  • -

#6 harryad2010  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 10-December 10

Re: C# file search problem

Posted 10 December 2010 - 07:59 AM

View PostCodeGrappler, on 10 December 2010 - 06:24 AM, said:

*EDIT* I see you posted while I was writing this up. I do realize the .NET framework has built in functionality to accomplish this however wheres the fun in not learning how to do it yourself? :)

You need to remove the console stuff from the SearchDirectories function. You should never ever call Main anywhere ever, It's just bad joojoo.

The real problem is since you are asking for input and calling Main in your recursive function whenever those foreach's finish for each directory. If you were to type close enough times you would eventually see the final output of your program.

This is my proposed solution that keeps your intended functionality in the order you had it.

using System;
using System.IO;
public class FileSearch
{
    public static void Main()
    {
        while(true)
        {
            Console.WriteLine("input name or part of the name.");
            string userinput1 = Console.ReadLine();
            Console.WriteLine("input location to search");
            string userinput2 = Console.ReadLine();
            String searchName = userinput1;
            DirectoryInfo myDir = new DirectoryInfo(@userinput2);
            SearchDirectories(myDir, searchName);

            Console.WriteLine("if you want to exit the program type close.");
            string close = Console.ReadLine();
            if (close.ToLower() == "close")
                break;
            
            Console.Clear();
        }
    }

    public static void SearchDirectories(DirectoryInfo dir, String target)
    {
        FileInfo[] files = dir.GetFiles();
        foreach (FileInfo file in files)
        {
            if (file.Name.IndexOf(target) > -1)
            {
                Console.WriteLine(file.Name);
            }
        }

        DirectoryInfo[] dirs = dir.GetDirectories();
        foreach (DirectoryInfo subDir in dirs)
        {
            SearchDirectories(subDir, target);
        }
    }
}



Ty for this, this helped me understand what i did wrong. :D
no more console handeling in the search function :P
Was This Post Helpful? 0
  • +
  • -

#7 mavarazo  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 37
  • View blog
  • Posts: 182
  • Joined: 25-October 10

Re: C# file search problem

Posted 10 December 2010 - 08:21 AM

As I said in the first response :)
Was This Post Helpful? 0
  • +
  • -

#8 CodeGrappler  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 41
  • View blog
  • Posts: 120
  • Joined: 29-November 10

Re: C# file search problem

Posted 10 December 2010 - 08:28 AM

View Postmavarazo, on 10 December 2010 - 07:21 AM, said:

As I said in the first response :)


Indirectly, yes you did. But you didn't explain to him why it was a problem.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1