Recursive Directory Listing

Tim Burrell tim at timburrell.net
Wed Feb 13 11:26:44 PST 2008


okibi wrote:
> Hello,
> 
> I was wondering if anyone could tell me how I would go about doing this. I need a function that can list all the files and directories in a given directory, and return an array that I can use that can reference each file, each folder, and each file within each subfolder. Is this possible?
> 
> Thanks!

tango.io.FileScan :).

If you're using Phobos, a recursive file scanner is pretty much the
easiest thing you can write!

Pseudo code:

scan(char [] Path, ref char [][] Paths) {
	Paths ~= Path

	if (!Path.isDirectory)
		return

	ListOfPaths = getContentsOfDir(Path)
	foreach (Item; ListOfPaths)
		scan(Path)
}

Obviously you can get more creative and store an array of structures
which contain file system information such as whether or not the path is
a file or a directory, etc, but that's the basic idea.

Even if you're not using Tango, you can always look at the source code
to see how they are doing it:
http://dsource.org/projects/tango/docs/current/source/tango.io.FileScan.html

Hope this helps!


More information about the Digitalmars-d-learn mailing list