Exemple #1
0
        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;
                    }
                }
            }
        }
Exemple #2
0
        /// <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);
        }
Exemple #3
0
        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);
        }