Uncategorized

Stack-based directory traversal

December 10, 2005

author:

Stack-based directory traversal

If you do any file processing, you will inevitably find yourself writing recursive code to traverse a directory structure. If you have a directory structure that is broad and deep with files/folders numbering in the thousands, recursion is inefficient as it consumes stack resources. In some cases you will run out of stack space and cause the code to throw an error.

You can do this more efficiently by using a seldom-used class of the System.Collections namespace: Stack. The Stack class implements a LIFO data structure (Last In First Out), as opposed to Queue, which implements FIFO (First In First Out). Instead of deferring to the run-time to implement the stack (which is essentially what recursion does, but in an inefficient way), you can optimize the process and also prevent your code from breaking, by managing the stack yourself.

Here is a simple example that will traverse and print-out all the files in a directory structure without using recursion:

private void TraverseFolder(DirectoryInfo dir)
{
	Stack stack = new Stack();
	stack.Push(dir);
	while (stack.Count > 0)
		{
			DirectoryInfo dirInfo = (DirectoryInfo) stack.Pop();
			foreach(FileInfo file in dirInfo.GetFiles())
			{
				System.Web.HttpContext.Current.Response.Write(file.FullName + "
"); } DirectoryInfo[] subDirs = dirInfo.GetDirectories(); for (int index = subDirs.Length - 1; index >= 0; index -= 1) stack.Push(subDirs[index]); } }

 

Founder NftyDreams; founder Decentology; co-founder DNN Software; educator; Open Source proponent; Microsoft MVP; tech geek; creative thinker; husband; dad. Personal blog: http://www.kalyani.com. Twitter: @techbubble
Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.