Find Files and folders

  • Thread starter Thread starter frogman7
  • Start date Start date
F

frogman7

what i am trying to do is something similar to the search that windows
does to find files and folders. I am putting a start directory and
want to find all the file (including the files in the sub folders) so i

can write each line to a master file. i have it working but it uses
loops and it extremely slow. Is there a function that grabs all the
files from a folder and subdirectories?
 
can write each line to a master file. i have it working but it uses
loops and it extremely slow. Is there a function that grabs all the
files from a folder and subdirectories?

How are you writing out to the file?

Are you concatenating the data before outputting?
 
I am using streamwriter at present. as i loop through and find the
file i write a string to the file with a return at the end of it. but
maybe that is why it is so slow
 
frogman7 said:
I am using streamwriter at present. as i loop through and find the
file i write a string to the file with a return at the end of it. but
maybe that is why it is so slow

How are you obtaining the directory information? That was the source
of slowdown when I tried something similar recently.
 
I basically get the top directory and find all the files in that
directory and parse them
then I find all the subdirectories and loop through them doing this
recursively until no more lists it is a while loop inside of a for loop
inside of a for loop but I need to add another loop at the top so it
will be a 4 loop system and that just seems really slow.

I am trying a new approach getting all directories and subs and putting
them in an arraylist then call the find file part using that arraylist.

Do you think this new approach will be faster?
 
frogman7 said:
I basically get the top directory and find all the files in that
directory and parse them
then I find all the subdirectories and loop through them doing this
recursively until no more lists it is a while loop inside of a for loop
inside of a for loop but I need to add another loop at the top so it
will be a 4 loop system and that just seems really slow.

I am trying a new approach getting all directories and subs and putting
them in an arraylist then call the find file part using that arraylist.

Do you think this new approach will be faster?

Actually, I meant what method or commands you were using to get the
directory info from the disk in the first place.

Some of those commands are rather slow, and if your new technique will
reduce calls to those commands, it may well help you out if that's the
bottleneck.

Check a recent thread here, it may have some info that helps you. The
code I posted does a recursive directory listing using two methods,
System.IO.DirectoryInfo and direct API calls, and includes benchmarking
results:

Title: What .NET classes are SLOW vs. API?
http://groups.google.com/group/micr...pi+methods+slow&rnum=3&hl=en#6e62e68f40049cc4
 
Sorry I didn't understand

I am using the system.IO.getfiles and getdirectories

I will look at your post and try it.

thanks
 
There are 2 points to be considered here.

The 1st is getting the information in the first place. One of the overloads
of System.IO.Directory.GetFiles allows searching sub-directories thus
allowing you to get all the filenames in a given tree in a single operation.

The 2nd point is that the result of a 'GetFiles' is an array of strings so
there is no need to put the results into another array or indeed write them
to a file individually. The System.String.Join method provides you with a
mechanism to write the entire array to a file in one operation.

In it's simplest terms it becomes:

File.WriteAllText([output filename], String.Join(Environment.Newline,
Directory.GetFiles([start point], "*.*", SearchOption.AllDirectories)))
 
Stephany said:
File.WriteAllText([output filename], String.Join(Environment.Newline,
Directory.GetFiles([start point], "*.*", SearchOption.AllDirectories)))

Wow, that's beautiful. :)

Frogman, it seems the API method I described only shows a speed
advantage if you're also interested in size/dates/attribs. If you just
need the name, Stephany's method matches or beats the speed, and blows
it away in simplicity.
 
Stephany said:
In it's simplest terms it becomes:

File.WriteAllText([output filename], String.Join(Environment.Newline,
Directory.GetFiles([start point], "*.*", SearchOption.AllDirectories)))
Well done Stephany! That is one of the simplest, yet at the same time
most functional, solution I've ever seen to the OP problem. That's what
can make these NG's such a special resource.

ShaneO

There are 10 kinds of people - Those who understand Binary and those who
don't.
 
Works great for me in VS2005. Should this code work with .net 1.1 I cannot
seem to get it to... perhaps you know what I'm doing wrong.. of it it can
work at all?

Thanks!

Stephany said:
File.WriteAllText([output filename], String.Join(Environment.Newline,
Directory.GetFiles([start point], "*.*", SearchOption.AllDirectories)))

Wow, that's beautiful. :)

Frogman, it seems the API method I described only shows a speed
advantage if you're also interested in size/dates/attribs. If you just
need the name, Stephany's method matches or beats the speed, and blows
it away in simplicity.
 
The technique demonstrated uses features introduced in .NET Framework 2.0.


vs 2003? said:
Works great for me in VS2005. Should this code work with .net 1.1 I
cannot
seem to get it to... perhaps you know what I'm doing wrong.. of it it can
work at all?

Thanks!

Stephany said:
File.WriteAllText([output filename], String.Join(Environment.Newline,
Directory.GetFiles([start point], "*.*", SearchOption.AllDirectories)))

Wow, that's beautiful. :)

Frogman, it seems the API method I described only shows a speed
advantage if you're also interested in size/dates/attribs. If you just
need the name, Stephany's method matches or beats the speed, and blows
it away in simplicity.
 
Back
Top