Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I perform full recursive directory & file scan?

here is my code:

    private static void TreeScan(string sDir)
    {
        foreach (string d in Directory.GetDirectories(sDir))
        {
            foreach (string f in Directory.GetFiles(d))
            {
                //Save file f
            }
        }
        TreeScan(d, client);
    }

The problem is that it doesn't get the FILES of the sDir (Starting Directory) it only gets the Folders and the Files in the Sub Folders.

How can I make it get the files from the sDir too ?

like image 571
Danpe Avatar asked Jul 07 '11 01:07

Danpe


2 Answers

Don't reinvent the wheel, use the overload of GetFiles that allows you to specify that it searches subdirectories.

string[] files 
    = Directory.GetFiles(path, searchPattern, SearchOption.AllDirectories);
like image 179
Anthony Pegram Avatar answered Sep 30 '22 03:09

Anthony Pegram


private static void TreeScan( string sDir )
{
    foreach (string f in Directory.GetFiles( sDir ))
    {
        //Save f :)
    }
    foreach (string d in Directory.GetDirectories( sDir ))
    {
        TreeScan( d ); 
    }
}
like image 42
Joel Lucsy Avatar answered Sep 30 '22 02:09

Joel Lucsy