469,271 Members | 997 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,271 developers. It's quick & easy.

Directory.GetFiles

I'm using Directory.GetFiles to enumerate files in a directory. The problem
is if you have to enumerate all files + subdirectories recursively, it takes
too much memory, and it fails.

Is there another way to enumerate files and subdirectories recursively which
doesn't take too much memory in CS?
Nov 16 '05 #1
3 30363
> I'm using Directory.GetFiles to enumerate files in a directory. The problem
is if you have to enumerate all files + subdirectories recursively, it takes
too much memory, and it fails.

Is there another way to enumerate files and subdirectories recursively which
doesn't take too much memory in CS?


How much is "too much" memory? How are you recursing? Are you storing just
the filename? Are you doing an entire drive? Can you provide a sample of
your code that exhibits the problem?

Nov 16 '05 #2
Here's the sample code. I set this program to scan the entire C: directory.
Eventually it gets terminated by the system with "out of memory" error.

static void ProcessInput(string inputpath)
{
//... stuff

string [] files = Directory.GetFiles(directory, wildcards);
foreach (string file in files)
{
if ( ! DoSomething(Path.Combine(directory, file) ) )
{
// file count increament
}
}
}
if (CommandLine.Recurse)
{
string [] dirs = Directory.GetDirectories(directory, "*.*");
foreach (string dir in dirs)
{
ProcessInput(Path.Combine(dir, wildcards));
}
}
}


"Patrick Steele [MVP]" <pa*****@mvps.zerospam.org> wrote in message
news:06**********************************@microsof t.com...
I'm using Directory.GetFiles to enumerate files in a directory. The problem is if you have to enumerate all files + subdirectories recursively, it takes too much memory, and it fails.

Is there another way to enumerate files and subdirectories recursively which doesn't take too much memory in CS?
How much is "too much" memory? How are you recursing? Are you storing

just the filename? Are you doing an entire drive? Can you provide a sample of
your code that exhibits the problem?

Nov 16 '05 #3
Hi S. Han,

Bear in mind that the files list contains the full filename, including
filepath.
For getting a list of files including files in subdirectories you can do
something like

string[] GetFiles(directory)
{
string[] files;
string[] subdirectories = Directory.GetDirectories(directory);
foreach(string s in subdirectories)
{
string[] tempfiles = += GetFiles(s) // requires array.copies
string[] temp = new string[tempfiles.Length + files.Length];
Array.Copy(files from files)
Array.Copy(files from tempfiles)
files = temp;
}

string[] tempfiles = Directory.GetFiles(directory);
string[] temp = new string[tempfiles.Length + files.Length];
Array.Copy(files)
Array.Copy(tempfiles)
return temp;
}

This code is untested and you will need to fix some minor points like the
Array.Copy part. You will also need to try/catch the getfiles and
getdirectory method as they will throw exceptions for forbidden
directories etc.

However I have successfully run the algorithm to return a list of over a
million filenames on an entire drive

On Wed, 19 Jan 2005 14:59:26 -0800, S. Han <@> wrote:
Here's the sample code. I set this program to scan the entire C:
directory.
Eventually it gets terminated by the system with "out of memory" error.

static void ProcessInput(string inputpath)
{
//... stuff

string [] files = Directory.GetFiles(directory, wildcards);
foreach (string file in files)
{
if ( ! DoSomething(Path.Combine(directory, file) ) )
{
// file count increament
}
}
}
if (CommandLine.Recurse)
{
string [] dirs = Directory.GetDirectories(directory, "*.*");
foreach (string dir in dirs)
{
ProcessInput(Path.Combine(dir, wildcards));
}
}
}


"Patrick Steele [MVP]" <pa*****@mvps.zerospam.org> wrote in message
news:06**********************************@microsof t.com...
> I'm using Directory.GetFiles to enumerate files in a directory. The problem > is if you have to enumerate all files + subdirectories recursively, it takes > too much memory, and it fails.
>
> Is there another way to enumerate files and subdirectories recursively which > doesn't take too much memory in CS?


How much is "too much" memory? How are you recursing? Are you storing

just
the filename? Are you doing an entire drive? Can you provide a sample
of
your code that exhibits the problem?



--
Happy Coding!
Morten Wennevik [C# MVP]
Nov 16 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by John Smith | last post: by
4 posts views Thread by Elmo Watson | last post: by
1 post views Thread by brian | last post: by
2 posts views Thread by OpticTygre | last post: by
3 posts views Thread by Michael | last post: by
3 posts views Thread by Michael Jackson | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.