/// <summary> /// Processes all files on the given <paramref name="path"/> matching the specified <paramref name="searchPattern"/>. /// </summary> /// <remarks> /// The input <paramref name="path"/> can be a file or a directory. /// </remarks> /// <param name="path">The root path to the file(s) to be processed.</param> /// <param name="searchPattern">The search pattern to be used. A value of <b>null</b> or <b>""</b> indicates that all files are a match.</param> /// <param name="proc">The method to call for each matching file.</param> /// <param name="recursive">Whether or not the <paramref name="path"/> should be searched recursively.</param> public static bool Process(string path, string searchPattern, FileProcessor.ProcessFileCancellable proc, bool recursive) { Platform.CheckForNullReference(path, "path"); Platform.CheckForEmptyString(path, "path"); Platform.CheckForNullReference(proc, "proc"); bool cancel; // If the path is a directory, process its contents if (Directory.Exists(path)) { ProcessDirectory(path, searchPattern, proc, recursive, out cancel); } // If the path is a file, just process the file else if (File.Exists(path)) { proc(path, out cancel); } else { throw new FileNotFoundException(String.Format(SR.ExceptionPathDoesNotExist, path)); } return cancel; }
/// <summary> /// Processes all files on the given <paramref name="path"/> matching the specified <paramref name="searchPattern"/>. /// </summary> /// <remarks> /// The input <paramref name="path"/> can be a file or a directory. /// </remarks> /// <param name="path">The root path to the file(s) to be processed.</param> /// <param name="searchPattern">The search pattern to be used. A value of <b>null</b> or <b>""</b> indicates that all files are a match.</param> /// <param name="proc">The method to call for each matching file.</param> /// <param name="recursive">Whether or not the <paramref name="path"/> should be searched recursively.</param> public static void Process(string path, string searchPattern, FileProcessor.ProcessFile proc, bool recursive) { Process(path, searchPattern, delegate(string filePath, out bool cancel) { cancel = false; proc(filePath); }, recursive); }
/// <summary> /// Returns the number of files in the specified directory that satisfies a given condition /// </summary> /// <param name="path"></param> /// <param name="searchParttern"></param> /// <param name="recursive"></param> /// <param name="condition"></param> /// <returns></returns> public static long Count(string path, string searchParttern, bool recursive, Predicate <string> condition) { long counter = 0; FileProcessor.Process(path, searchParttern, delegate(string file) { if (condition == null || condition(file)) { counter++; } }, recursive); return(counter); }
private static void GetDirectories(string path, string searchPattern, FileProcessor.ProcessFileCancellable proc, bool recursive, out string[] dirList) { dirList = null; try { dirList = Directory.GetDirectories(path); } catch (Exception e) { Platform.Log(LogLevel.Warn, e); throw; } if (recursive) { foreach (string dir in dirList) { bool cancel; ProcessDirectory(dir, searchPattern, proc, recursive, out cancel); if (cancel) break; } } }
private static void ProcessDirectory(string path, string searchPattern, FileProcessor.ProcessFileCancellable proc, bool recursive, out bool cancel) { cancel = false; // Process files in this directory string[] fileList; GetFiles(path, searchPattern, out fileList); if (fileList != null) { foreach (string file in fileList) { proc(file, out cancel); if (cancel) return; } } // If recursive, then descend into lower directories and process those as well string[] dirList; GetDirectories(path, searchPattern, proc, recursive, out dirList); }