Get all files in all directories

  • Thread starter Thread starter TOI DAY
  • Start date Start date
T

TOI DAY

I need a routine to get all the files in the root dir and all
directories and subdirectories.

For example: I have these directories structure

C:\
Dir1
subdir1
file1.txt
file2.txt
Dir2
Subdir2
subsubdir2
file3.txt
file4.txt
test.doc
test2.doc

and so on

Can anyone help out with the code?

Thanks in advance
 
TOI DAY said:
I need a routine to get all the files in the root dir and all
directories and subdirectories.

Right. Use DirectoryInfo.GetFiles and DirectoryInfo.GetDirectories, and
either keep a list of all the directories you still need to search, or
recurse.
 
this code should get you started

public static void ProcessDirectory(string targetDirectory)

// Process the list of files found in the directory
string [] fileEntries = Directory.GetFiles(targetDirectory)
foreach(string fileName in fileEntries
ProcessFile(fileName)

// Recurse into subdirectories of this directory
string [] subdirectoryEntries = Directory.GetDirectories(targetDirectory)
foreach(string subdirectory in subdirectoryEntries
ProcessDirectory(subdirectory)


ht
jayson
 
Back
Top