5

I'm stuck at this problem I would really appreciate if someone can help me solve this problem.

  1. I want to add spaces for sub-folder like this format down below.(it must be done with recursion)
  2. I want to add spaces for sub-folder like this format down below.(it must be done with recursion)

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.IO;
    
    namespace G14_211115
    {
         class Program
         {
               static void Main(string[] args)
               {
                    string path = @"C:\Program Files\FinchVPN";
    
                    WriteDirectories(path);
                    Console.ReadKey();
               }
    
               /* Tasks.
               * 1. I want to add spaces for subfolder like this format down below. ( it must be done with recursion) 
               *    Like this
               *    --------------------------------------------------------------
               *    Folder 1
               *      Folder 1.1
               *      Folder 1.2
               *    Folder 2
               *      Folder 2.1
               *        Folder 2.1.1
               *      Folder 2.2
               *    Folder 3
               *    Folder 4
               *   
                * 2. Task 2 I want to retype this code without using recurrence and C# inbuilt functions.
               */
    
               static void WriteDirectories(string path)
               {
                    string[] dirs = Directory.GetDirectories(path);
    
                    for(int i = 0; i < dirs.Length; i++)
                    {
                         Console.WriteLine(dirs[i]); 
                         WriteDirectories(dirs[i]);
                    }
              }
         }
    }
    

2 Answers2

6

As you are calling WriteDirectories recursivly, you can pass a variable to each call indicating the (recursion)level. Using this you can just put some spaces before the output.

Therefore you would need to modify your code like this:

static void WriteDirectories(string path, int level = 0)
{
    string[] dirs = Directory.GetDirectories(path);

    for (int i = 0; i < dirs.Length; i++)
    {
        for (int j = 0; j < level; j++)
        {
            Console.Write(" ");
        }

        Console.WriteLine(dirs[i]);
        WriteDirectories(dirs[i], (level + 1));
    }
}

If you want more than just one space per level you can simply modify the inner loop, j < 2 * level would give you 2 space characters per level, and so on.

Update (Thanks to David)

Instead of the inner loop you can use new string(' ', level) for creating a new string containing the specified character level-times.

So the inner for-loop would be replaced by Console.Write(new string(' ', level));.

Markus Safar
  • 6,324
  • 5
  • 28
  • 44
  • 1
    Instead of the inner `for` loop, just do `Console.Write(new string(' ', level));` – DavidG Nov 26 '15 at 13:04
  • @DavidG I know that of course but I am pretty sure this is a homework and he is not on that level right now ;-) – Markus Safar Nov 26 '15 at 13:08
  • If it's homework then don't answer it, don't use it as an excuse to show "lower" quality code. – DavidG Nov 26 '15 at 13:11
  • Markus this is not homework this was homework once upon a time. I was studying C# 2 year ago and than I had a pause. I forgot everything right now. I was saving all homeworks and not I want to begin it from the start. thanks for answering btw. what about doing this without recurrence and inbuilt functions? – Nikusha Kalatozi Nov 26 '15 at 13:17
  • @NikushaKalatozi Solving this issue without recursion can be done by using a loop and a data structure like a stack. You can look at [http://stackoverflow.com/a/531704/2921691](this) answer for example. – Markus Safar Nov 26 '15 at 13:21
  • What about this code? the allign is wrong for it... it's not like Folder 1 * Folder 1.1 * Folder 1.2 it's like Folder 1 * Folder 1.1 * Folder 1.2 http://postimg.org/image/zdodzz7q3/ – Nikusha Kalatozi Nov 26 '15 at 13:29
  • 1
    @NikushaKalatozi Yeah, that's a little bug... replace `WriteDirectories(dirs[i], (++level));` with `WriteDirectories(dirs[i], (level + 1));` and it will work. I' ve updated the answer. – Markus Safar Nov 26 '15 at 13:33
0

just provide a depth-parameter to your recursive call and add spaces according to the depth.

       static void WriteDirectories(string path, int depth) {
            string[] dirs = Directory.GetDirectories(path);
            for(int i = 0; i < dirs.Length; i++) {
                 string preSpaces = new String(' ',depth);
                 Console.WriteLine(preSpaces + dirs[i]); 
                 WriteDirectories(dirs[i], depth+1);
            }
       }

Your first call should provide 0 or 1 as depth.

K. Berger
  • 361
  • 1
  • 9