SML, 176 bytes
open OS.FileSys;val! =chDir;fun&n w=(print("\n"^w^n);!n;print"/";c(openDir(getDir()))(w^"\t");!"..")and c$w=case readDir$of SOME i=>(&i w handle _=>();c$w)|x=>()fun%p=(&p"";!p)
Declares (amongst others) a function %
which takes a string as argument. Call with % "C:/Some/Path";
or % (getDir());
for the current directory.
I'm using the normally rather functionally used language StandardML whose FileSys
-Library I discovered after reading this challenge.
The special characters !
, &
, $
and %
have no special meaning in the language itself and are simply used identifiers; however the can't be mixed with the standard alphanumeric identifiers which allows to get rid of quite some otherwise needed spaces.
open OS.FileSys;
val ! = chDir; define ! as short cut for chDir
fun & n w = ( & is the function name
n is the current file or directory name
w is a string containing the tabs
print ("\n"^w^n); ^ concatenates strings
! n; change in the directory, this throws an
exception if n is a file name
print "/"; if we are here, n is a directory so print a /
c (openDir(getDir())) (w^"\t"); call c with new directory and add a tab to w
to print the contents of the directory n
! ".." we're finished with n so go up again
)
and c $ w = 'and' instead of 'fun' must be used
because '&' and 'c' are mutual recursive
$ is a stream of the directory content
case readDir $ of case distinction whether any files are left
SOME i => ( yes, i is the file or directory name
& i w handle _ => (); call & to print i an check whether it's a
directory or not, handle the thrown exception
c $ w ) recursively call c to check for more files in $
| x => () no more files, we are finished
fun % p = ( % is the function name,
p is a string containing the path
& p ""; call & to print the directory specified by p
and recursively it's sub-directories
! p change back to path p due to the ! ".." in &
)
Can be compiled like this with SML/NJ or with Moscow ML* by prefixing with load"OS";
.
*See mosml.org
, can't post more than 2 links.