CompSci 6
Fall 2008
Program Design and Analysis

Listing Directories

Files on a computer are organized hierarchically in directories. Because directories can contain files or other directories, which can contain more files and more directories, the structure itself is recursive. This structure makes using recursive algorithms a natural technique for processing it.

Consider the following problems with regards to directories on your computer.

  1. List all files in a directory recursively. Visit all of the files in the given directory and its sub-directories and return a list of File objects.

  2. List all files in a directory recursively, but only up to a certain depth. Instead of following contained directories indefinitely, one might want to only list files to a certain depth, that is the distance measured in nested sub-directories from the starting directory.

  3. Compute the total size of all files in a directory recursively. Visit all of the files in the given directory and its sub-directories and return an int representing the sum of the size of all the files found.

  4. Compute the maximum sized file in a directory recursively. Visit all of the files in the given directory and its sub-directories and return an int representing the largest size of all the files found.

  5. Print list of directories, with sub-directories indented below their containing directory. For a directory, all the files in that directory should be printed directly below it, indented by a few spaces. This means, for example, that a directory named maindir containing files named file1, file2, file3, file4 and subdirectories subdir1, subdir2, subdir3, subdir4 might be printed as follows (where each sub-directory has files as shown).

       maindir
          file1
          subdir1
            xxx
              foo
              bar
            yyy
          file2
          file3
          subdir2
            zzz
            ppp
          subdir3
            ooo
          file4
          subdir4
            www
            nnn