// Allison Obourn
// CS 142, Spring 2024
// Lecture 20

// Prints out the name of the directory this program is in and then the names
// of all files and folders inside it, all files and folders in its folders,
// etc. Files and folders are output one per line and all files are indented
// by one more tab than their containing folder.

import java.io.*;
import java.util.*;

public class Crawler {
   public static void main(String[] args) {
      // . means the current directory. That means, whatever directory you
      // put this program in is the one whose contents will be output.
      crawl(new File("."));
   }
   
   // Prints the name of the passed in file and all the files inside of it
   // If there are folders in it their contents is output too and their folders
   // folders folders etc until everything is output. Everything is indented one
   // more tab right than the folder that contains it.
   public static void crawl(File f) {
      crawl(f, "");
   }
   
   // prints the nam of the passed in file at the passed in indentation. If the
   // file is a folder outputs its contents one indentation further right. If 
   // it contains a folder, that folders contents is output an additional tab 
   // to the right etc.
   private static void crawl(File f, String indent) {
      // output the current file/folder name at current indent level
      System.out.println(indent + f.getName());
      if(f.isDirectory()) {
         // if this is a directory it could contain other files/folders so get
         // all of them and print them and their contents out
         File[] files = f.listFiles();
         for(int i = 0; i < files.length; i++) {
            // print all the files/folders in this file/folder. Since they are 
            // inside, indent them one further to the right
            crawl(files[i], indent + "\t");
         }
      }
   }
}