user3258394
user3258394

Reputation: 27

Split directories using recursion and string.split

Here's my problem. I'm using C# and I'm supposed to design a program that runs a directory and list all of the sub-directories and files within. I've got the basic code down, which is shown below:

namespace DirectorySearch
{
    class DirectorySearch
    {
        const string PATH = @"C:\Directory Search";

        static void Main(string[] args)
        {
            DirSearch(PATH);
        }

        static void DirSearch(string dir)
        {
            try
            {
                foreach (string f in Directory.GetFiles(dir))
                    Console.WriteLine(f);
                foreach (string d in Directory.GetDirectories(dir))
                {
                    Console.WriteLine(d);
                    DirSearch(d);
                }

            }
            catch (System.Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
    }
}

The problem I'm having is I'm supposed to use stringName.Split('/').Last so the output is all in one straight line, but I have no idea where to place it in the code. The output is supposed to look like:

C:\Directory Search
  File1.txt
  File2.txt
    Folder1
    Folder2
      File3.txt
    Folder3

and so on... I'm pretty sure I can figure out how to make the tabs work after I figure out how to split them up. Any help would be greatly appreciated.

Upvotes: 0

Views: 500

Answers (3)

Jade
Jade

Reputation: 2992

I'm Not sure if you'r looking for something like this, but you can try this code

namespace DirectorySearch
{
    class DirectorySearch
    {
        const string PATH = @"C:\Directory Search";

        static void Main(string[] args)
        {
            Console.WriteLine(PATH);
            DirSearch(PATH);
        }

        static void DirSearch(string dir, int depth = 1)
        {
            try
            {
                string indent = new string('\t', depth);
                foreach (string f in Directory.GetFiles(dir))
                    Console.WriteLine(indent + f.Split('\\').Last());//using split
                    //Console.WriteLine(indent + Path.GetFileName(f));//using Get File name

                foreach (string d in Directory.GetDirectories(dir))
                {
                    Console.WriteLine(indent + d.Split('\\').Last()); //using split
                    //Console.WriteLine(indent + Path.GetFileName(d)); //using Get File name
                    DirSearch(d, depth++);
                }

            }
            catch (System.Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
    }
}

Hope this helps you!

Upvotes: 1

TGH
TGH

Reputation: 39258

Try this

Console.WriteLine(f.Split('\\').Last());

Upvotes: 1

Selman Genç
Selman Genç

Reputation: 101701

You just need to use Path.GetFilename which returns directory name (for directories) or file name with extension

foreach (string f in Directory.GetFiles(dir))
        Console.WriteLine(Path.GetFileName(f));

foreach (string d in Directory.GetDirectories(dir))
{
     Console.WriteLine(Path.GetFileName(d));
     DirSearch(d);
}

Upvotes: 1

Related Questions