SML and recursion

Jodell88

Diamond Member
Jan 29, 2007
8,762
30
91
My program is supposed to read a given directory and lists it's contents into a text file. I can get my program to write one file's attributes to a file but i can't do it recursively.

My code

(*Imports structures that are used by the program*)
open TextIO;
open OS.FileSys;
open Option;
open Date;

fun listdir(filename, directoryname) =
let
val opener = openDir(directoryname)
val filePath = openAppend(filename)
in
(listdirHelper(filePath,opener); flushOut(filePath); closeOut(filePath))
end;

fun listdirHelper(fName,x) =
let
val read = readDir(x)
val vo = valOf(read)
val fTime = modTime(vo)
val size = fileSize(vo)
val timeLocal = fromTimeLocal(fTime)
val modDate = fmt "%d-%B-%Y" (timeLocal)

in
(output(fName,vo ^ " " ^ modDate ^ "" ^ size ^"\n"); listdirHelper(fName,x))
end;


 

Ken g6

Programming Moderator, Elite Member
Moderator
Dec 11, 1999
16,835
4,815
75
I don't really know Standard ML either, but I've been looking over this and the Wikipedia page. I assume there's some way to get a list of the files in the directory? If so, then operate on one entry in that list with listdirHelper, and call it recursively with the list minus the element you worked with, as in the mergesort example.