/// <summary> /// Just gets a string list of all files in a path /// path = the path to search (duh!) /// gt = search type 0 is files only, with setting 1 directories can be listed as well, 2 will list directories only, and 3 will generate a tree with all directories. /// hidden = Allow hidden files in the search results. Please note, hidden means by UNIX STANDARDS, so it will only check if a file is prefixed with a "." or not! /// </summary> static public string[] GetDir(string path, int gt = 0, bool sorted = true, bool hidden = false, bool allowsymlinks = true) { // init FLError = ""; var w = new List <string>(); var di = new DirectoryInfo(Dirry.AD(path)); // Check if (!di.Exists) { FLError = "TrickyFileList.FileList.GetDir(\"" + path + "\"," + gt + "," + "," + sorted + "," + hidden + "): Directory does not exist!"; return(null); } // Listout foreach (FileInfo fi in di.GetFiles()) { if ((gt == 0 || gt == 1 || gt == 3) && (hidden || fi.Name.Substring(0, 1) != ".") && (allowsymlinks || (!IsSymbolic($"{path}/{fi.Name }")))) { w.Add(fi.Name); } } foreach (DirectoryInfo fi in di.GetDirectories()) { if (hidden || fi.Name.Substring(0, 1) != ".") { switch (gt) { case 1: case 2: w.Add(fi.Name); break; case 3: var gd = GetDir(path + "/" + fi.Name, 3, false, hidden); if (gd == null) { return(null); // Error catching. FLError has already been defined so no need to do that again! } foreach (string nf in gd) { w.Add(fi.Name + "/" + nf); } break; } } } // Sort if asked if (sorted) { w.Sort(); } // return the crap return(w.ToArray()); }
//static StringBuilder StaticLevelBuilder = new StringBuilder(); // I chose this way in order not to have to allocate and release too much memory.... Slows things down, needlessly! static public List <string> Tree(string Expression, CheckCase CC = CheckCase.Auto, Type t = Type.FilesOnly, bool hidden = false) { var ret = new List <string>(); var Ex = Dirry.AD(Expression); var ExA = Ex.Split('/'); //var start = 0; var root = false; var drive = false; /* Maybe this wasn't the right approach... It's always a puzzle :) * string level(int l) { * StaticLevelBuilder.Clear(); * for (int i = 0; i <= l && i < ExA.Length) { * if (i > 0) StaticLevelBuilder.Append('/'); * StaticLevelBuilder.Append(ExA[i]); * } * return StaticLevelBuilder.ToString(); * } */ void ArrayPrefix(string[] d, string Prefix) { for (int i = 0; i < d.Length; i++) { d[i] = $"{Prefix}{d[i]}"; } } try { string[] VL = null; if (ExA.Length == 0) { return(ret); // No need to do anything! } if (ExA[0].Length == 2 && ExA[0][1] == ':') { ret.Add(ExA[0]); drive = true; /*start = 1;*/ VL = FileList.GetTree($"{ExA[0]}/", true, hidden); } else if (ExA[0].Length == 0) { root = true; /*start = 1;*/ VL = FileList.GetTree("/", true, hidden); } else //if ((!root) && (!drive)) { { Debug.WriteLine($"No root or drive, so let's get 'em ... Reading tree {Directory.GetCurrentDirectory()}"); VL = FileList.GetTree(Directory.GetCurrentDirectory(), true, hidden); Debug.WriteLine($"{VL.Length} entries found"); } foreach (string entry in VL) { //Debug.WriteLine($"Matching: {entry} / {Expression}"); if (Regex.IsMatch(entry, $"^{Expression}$")) #if DEBUG { Debug.WriteLine($" MATCH FOR: {entry} / {Expression}"); #endif { ret.Add(entry); } #if DEBUG } else { Debug.WriteLine($"NO MATCH FOR: {entry} / {Expression}"); } #endif } } catch (Exception e) { Console.Beep(); Console.Beep(); Console.Beep(); Debug.WriteLine($"RegExTree(\"{Expression}\",CheckCase.{CC},Type.{t},{hidden}): Exception thrown {e.Message}"); } return(ret); }