Click here to Skip to main content
15,880,651 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi sir,
Tell me the steps to put the progress bar value in a recursive function. Consider the following scenarios, i have one folder path "d:\studentinfo" , it contains many subfolders, for that i have to search the pdf files in folders and subfolders of the folder path, i dont know how many files before the process starts. I need to show the progress bar, displaying how much process is completed. I have a recursive function, that search the pdf files in that folder path recursively, my requirement is to display the progress bar in that recursive function.

I shared my recursive function for your reference. Kindly give me the solution to get the progress bar in a recursive function.


C#
//this is the recursive function
        public static void getDirsFiles(DirectoryInfo d)
        {
         //   this.backgroundWorker1.ReportProgress(i);
          //  System.Threading.Thread.Sleep(500);
            //create an array of files using FileInfo object
            FileInfo[] files;
            //get all files for the current directory
            files = d.GetFiles("*.pdf");

            //iterate through the directory and print the files
            foreach (FileInfo file in files)
            {
                i++;
                //get details of each file using file object
                String fileName = file.FullName;
                String fileSize = file.Length.ToString();
                String fileExtension = file.Extension;
                String fileCreated = file.LastWriteTime.ToString();

              //  HttpContext.Current.Response.Write(fileName + " " + fileSize + " " + fileExtension + " " + fileCreated + "<br><br>");
            }

            //get sub-folders for the current directory
            DirectoryInfo[] dirs = d.GetDirectories("*.*");

            //This is the code that calls 
            //the getDirsFiles (calls itself recursively)
            //This is also the stopping point 
            //(End Condition) for this recursion function 
            //as it loops through until 
            //reaches the child folder and then stops.

            //HttpContext.Current.Response.Write("<br>");

            foreach (DirectoryInfo dir in dirs)
            {
              //  HttpContext.Current.Response.Write("--------->>  " + dir.Name);
                getDirsFiles(dir);
            }

        }
Posted
Updated 18-Aug-14 23:18pm
v2

Why are you doing this recursively?
Instead, try this:
C#
string[] files = Directory.GetFiles(@"D:\Temp\", "*.pdf", SearchOption.AllDirectories);
And it will return you all the PDF files in the whole branch, as strings with the complete file path.
Then you know how many there are before you even start processing them...
 
Share this answer
 
You could show progress based on the number of folders in the first folder. If, for example, there are 10 folders in studentinfo then every folder accounts for 10% of progress. So on the first entry of the recursive function it needs to do some extra work of storing the number of folders and updating the progress bar.

This should give you some idea on how to complete this assignment. ;-)

Good luck!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900