2016-05-24 5 views
0

Ok, donc voici ma question actuellement:Obtenir une liste des sous-dossiers dans un répertoire

Ceci est mon principal:

//System prompts user the type of archiving which will direct to the correct directory to set as root 
Console.WriteLine("Enter the type of archiving you would like to do?"); 
string archivetype = Console.ReadLine(); 

//function that identifies which candidates to archive 
Archive(archivetype, 20, 20); 

//keep the application in debug mode 
Console.ReadKey(); 

La méthode d'archivage:

//Archive method determines which directory to search in and how many versions to archive 
static void Archive(string archivetype, int pversion, int version) 
{ 
    //regex pattern to get folder names of the type #.#.#.#/#. something 
    Regex reg = new Regex(@"\d+(\.\d+)+"); 
    //setting where to start looking 
    DirectoryInfo root = new   DirectoryInfo(@"C:\Users\jphillips\Desktop\test\parent\ACE-3_0"); 
    var dirs = new List<DirectoryInfo>(); 
    //i want to make a recursive call to all the folders in my root directory to obtain all the folders with the regex pattern above that are not empty and do not have 3 files inside 
    WalkDirectoryTree(root);  
} 

enfin à pied méthode directorytree

//so im using the walk directory tree on the microsoft website i need a way to have a sort of a global array to keep adding the directories that fit through the patterns mentioned above without resetting itself after each walkdirectorytree call 
static void WalkDirectoryTree(System.IO.DirectoryInfo root) 
{ 
    DirectoryInfo[] subDirs = null; 

    // Now find all the subdirectories under this root directory. 
    subDirs = root.GetDirectories(); 

    foreach (DirectoryInfo dir in subDirs) 
    { 
     //dirs is not global so it doesnt work here and i believe if i put a local array that it will reset itself everytime 
     dirs = root.GetDirectories("*", SearchOption.TopDirectoryOnly).Where(d => reg.IsMatch(d.Name)).ToList(); 
     if() 
     WalkDirectoryTree(dir); 
    } 
} 

donc im vraiment perdu à ce stade je veux pouvoir appeler walkdirectorytree pour parcourir tous mes dossiers et sous-dossiers de mon répertoire recursevely pour extraire les chemins qui ont le motif regex et qui ne sont pas vides et n'ont pas 3 fichiers à l'intérieur tp donner moi une liste de ces chemins de dossiers.

Répondre

1

Vous pouvez obtenir tous les dossiers et sous-dossiers en un seul appel avec cette surcharge de GetDirectories.

Vous passez dans une chaîne de recherche - mais pas une regex malheureusement et SearchOption.AllDirectories comme deuxième argument. Vous pouvez ensuite passer les résultats à travers votre regex pour trouver ceux qui vous intéressent.

+0

OK donc c'est ce que je vais faire: dirs = root.GetDirectories ("*", SearchOption.AllDirectories) .Where (d => reg.IsMatch (d.Name)). ToList(). Sélectionnez (w => w.); – PhillipsJP

+0

dans la sélection comment je voudrais supprimer par exemple si mon dossier a TESTS à la fin de celui-ci. donc un format normal serait 4.3.2.4 mais certains dossiers ont 4.45.3.3_TESTS – PhillipsJP

+0

@PhillipsJP Obtenir une expression rationnelle de travail pour ce cas pourrait valoir une question distincte. – ChrisF