473,385 Members | 1,707 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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 30557
> 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: John Smith | last post by:
Hello all: I am trying to search for more than one extension in a directory at the same time with the following code: string files = Directory.GetFiles(sDir, "*.doc*.dot"); However, this...
4
by: Elmo Watson | last post by:
Is there a way, with the System.IO class, to do a recursive list of a directory structure? For instance, in DirectoryInfo, you have GetDirectories and GetFiles .... In Directory, you have...
1
by: brian | last post by:
I am using the Directory.GetFiles class and am having problems with the overloaded version. I want to search files. I can use the following and it works fine: Directory.GetFiles(Path, k) Path:...
2
by: OpticTygre | last post by:
I have a directory of 27 files. The files end in either 1.txt, 2.txt, 3.txt, or 4.txt. If I say: For Each filename As String In...
3
by: Michael | last post by:
Hi, I have been using Directory.GetFiles("g://"); in my c# application. But now my boss want's me to do Directory.GetFiles(IP address); I have been unable to figure out how to get this to work....
3
by: cjb | last post by:
Is there a way to get Directory.GetFiles to return multi-language file names? I haven't found any overloads that allow any such parameter. The way I am using it now is: foreach (string d in...
2
by: Alex | last post by:
Hello, I'm creating a program to parse a directory and all files within that directory (including subdirectories), but 'directoryinfo' only parses the current directory. Is it possible to have...
3
by: Michael Jackson | last post by:
In my .NET 2.0 VS 2005 VB application, I'm using Directory.GetFiles(path) to get all the files in the directory. However, I'm getting an error regarding "Illegal character in Path", even though I...
0
by: tshad | last post by:
I am trying to do multiple Directory.GetFiles and append the results to one array that I will process. I tried this: string strFiles; strFiles = Directory.GetFiles(SemaSettings.InputFilePath,...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.